【问题标题】:React-router You cannot push the same route... force History.push / reload componentReact-router 你不能推送相同的路由... force History.push / reload component
【发布时间】:2017-05-31 15:58:41
【问题描述】:

我正在尝试让我的搜索栏正常工作。
进行搜索时,路由器会将我发送到 /list/(无论我搜索什么)

搜索栏正在以编程方式更改我的路线,如下所示:

var pathname = '/list/';
        var query = {
            q: this.state.searchBar
        };
        hashHistory.push({pathname, query});  

/list/:q 有一个 componentDidMount 函数,该函数将 GET 请求发送到数据库,但如果我在 '/list/:q' 上已经准备好进行新搜索,则 url 会更新,但不会重新加载组件。

我的问题: 在搜索路线 /'list/:q' 上进行新搜索时,如何强制我的组件重新加载?

相关代码:
Pastebin link

【问题讨论】:

  • 你试过使用componentWillReceiveProps吗?
  • 做到了!谢谢。看来我需要更好地阅读生命周期方法。

标签: reactjs react-router


【解决方案1】:

正如 Paul S 建议的那样,使用 componentWillReceiveProps 解决了我的问题。

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2019-01-15
    • 1970-01-01
    • 2016-10-20
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多