【问题标题】:history.push redirect overrides the the path I set to different onehistory.push 重定向覆盖了我设置为不同的路径
【发布时间】:2021-02-07 23:48:03
【问题描述】:

我有这个小部件

整个东西都可以点击到某个路径。我在里面放了两个按钮,编辑和删除

remove 工作正常,它执行一个传奇。现在编辑按钮我希望它是一个重定向到某个 URL 的链接。我不能这样做,因为我不能将<a> 放在<a> 中。 React 向我显示警告。

所以我使用了一个重定向按钮:

1.

import {Link, useHistory} from "react-router-dom";
const history = useHistory();
    <button onClick = {() => history.push(`/domain/${props.domain.id}/edit`)}>
    edit
    =</button>
  1. 因为我用的是connected-react-router,所以我也试过

    import {history} from '../../../redux/store'; &lt;button onClick = {() =&gt; history.push(/domain/${props.domain.id}/edit)}

它们都有相同的行为:

您可以使用“/domain/5f95554624aa9a0006351825”覆盖重定向,这是外部链接的链接。

即使我将 url 更改为“/users”之类的内容,它仍然会被覆盖。

我只想将用户重定向到简单的 URL。我该怎么做?

【问题讨论】:

    标签: reactjs redirect react-router react-router-dom connected-react-router


    【解决方案1】:

    您可以使用preventDefault 来停止Link 的默认行为。

    <Link to="/xyz">
      Link
      <button
        onClick={(e) => {
          history.push(`/domain/${props.domain.id}/edit`)
          e.preventDefault()
        }}
      >
        edit
      </button>
    </Link>
    

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多