【问题标题】:Why history.push does not work in route "/" in react?为什么history.push 在路由“/”中不起作用?
【发布时间】:2021-11-02 19:45:52
【问题描述】:

在一个 React 项目中,我使用了react-router-dom。 我的问题是为什么在路线“/”history.push 中不起作用?

  this.props.history.push({
                    pathname:"/searchpost",
                    data:data,
                    backg: c,
                    texts:  " tags"+t,
                });

【问题讨论】:

  • 问题需要更清楚
  • @SachinAnanthakumar 路径名已包含在状态对象中。那肯定是错的。

标签: javascript reactjs laravel


【解决方案1】:

大家可以参考这篇文章:https://codesource.io/how-to-use-this-props-history-push-on-your-react-project/

它声称该方法有两个参数:

  • 路径
  • [状态]

state 参数是可选的,但它必须是一个对象。示例:


class MyComponent extends React.Component {
  //...
  myFunction() {
    this.props.history.push("/dashboard", { state: "sample data" });
  }
  //...
}

export default withRouter(MyComponent);

因此,您的错误是您将路径包含在状态中,因此 function 不知道去哪里。也许这会更好:


this.props.history.push("/searchpost", {
  data: data,
  backg: c,
  texts: " tags" + t,
});

我对 React 不是很有经验,所以如果我完全错了,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2022-01-23
    相关资源
    最近更新 更多