【问题标题】:No able to redirect to home page with React Router Dom无法在反应中重定向到主页
【发布时间】:2022-01-11 14:07:42
【问题描述】:

当用户点击EDIT 图标时,将打开一个页面。在用户submit 记录之后,如果成功,我们的页面应该重定向到ListCertificate

当用户点击编辑按钮时,下面的行将运行。并打开编辑表单。

<Link to={`/${props.certificate.id}/edit` }>Edit</Link>

用户保存成功后,我们的页面应该重定向到ListCertificate组件。

所以我写了

 .then(function (response: any) {
      console.log(response);
      toast("???? Record updated successflly", {
        position: "top-right",
        autoClose: 2000,
        hideProgressBar: false,
        closeOnClick: true,
        pauseOnHover: true,
        draggable: true,
        progress: undefined
      });
       return <Redirect  to='/listcertificate'  />
    })

这条线不工作。

return <Redirect  to='/listcertificate'  />

在 app.js 中,我使用下面的方法调用了 ListCertificate。

<Route  path ="/listcertificate" component={Listcertificate} ></Route>

localhost:4200/listcertificate 工作正常。但它也没有从editcomponent重定向到同一页面。

【问题讨论】:

  • 你使用什么版本的 react-router-dom?
  • 谢谢.........它是 6.0.2

标签: javascript reactjs typescript react-router


【解决方案1】:

使用react-router-dom v6,您可以使用useNavigate 挂钩以编程方式进行导航。 Docs

import { useNavigate } from "react-router-dom";
..
const navigate = useNavigate();
...
.then(function (response: any) {
      console.log(response);
      toast("? Record updated successflly", {
        position: "top-right",
        autoClose: 2000,
        hideProgressBar: false,
        closeOnClick: true,
        pauseOnHover: true,
        draggable: true,
        progress: undefined
      });
       navigate("/listcertificate", { replace: true });
    })

【讨论】:

    【解决方案2】:

    当您不在JSX 时,使用useHistory 而不是Redirect

    import {useHistory} from "react-router-dom"
    
    const history = useHistory();
    history.push('/listcertificate');
    

    如果你使用的是react-router-dom的第6版,你可以使用useNavigate

     import {useNavigate} from "react-router-dom";
     
     const navigate = useNavigate();
     
     navigate("/listcertificate", { replace: true });
    

    【讨论】:

    • 它会立即重定向。我的意思是当我点击编辑按钮时,它会直接列出证书。
    • history.push('/listcertificate') 应该是你有return &lt;Redirect to='/listcertificate' /&gt; 的地方,是你做的吗?
    • 仅适用于 react-router-dom v5,v6 必须使用useNavigate hook
    • @VitaliyRayets 我更新了我的回复。没想到他使用的可能是react-router-dom的第6版。谢谢!
    【解决方案3】:

    您忘记在&lt;Navigate/&gt; 中添加replace 属性。

    &lt;Navigate replace&gt; 属性告诉路由器在更新 URL 时使用history.replaceState(),这样 / 条目就不会出现在历史堆栈中。这意味着当有人点击后退按钮时,他们最终会回到他们导航到/ read more

    之前的页面

    改变这个

    &lt;Redirect to='/listcertificate' /&gt;

    到这里

    &lt;Navigate to='/listcertificate' replace /&gt;

    或者到这个

    &lt;Navigate to='/listcertificate' replace={true} /&gt;

    【讨论】:

    • A &lt;Navigate&gt; 元素在 JSX 中呈现时会更改当前位置,但 @Niharika Suse 在 JSX 外部的函数回调中调用重定向。
    • RRDv6 中没有Redirect 组件,它也没有replace 属性,但是使用替换它的Navigate 组件仍然会遇到同样的问题。 OP 需要命令式导航,而不是声明式导航。
    • @DrewReese 谢谢你提到我的错误,我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 2012-02-05
    • 1970-01-01
    • 2016-10-10
    • 2021-12-18
    • 2015-09-30
    • 2019-05-26
    • 2021-06-05
    相关资源
    最近更新 更多