【问题标题】:React Router v4 not navigating to historical pages using back/forward buttonsReact Router v4 不使用后退/前进按钮导航到历史页面
【发布时间】:2017-06-04 20:09:01
【问题描述】:

我正在使用react-router-dom: v4.1.1,这是我的路由器代码:

var ReactRouter = require('react-router-dom');
var Router = ReactRouter.BrowserRouter;
var Route = ReactRouter.Route;
var Switch = ReactRouter.Switch;
var browserHistory = ReactRouter.BrowserHistory;

  <Router history={browserHistory}>
    <div className='container'>
<Switch>
    <Route exact path='/' render={(props) => <Home
      {...props} // for router history and all that.
      rebase={rebase}
      items={this.state.items}
      user={this.state.currentUser}
      userData={this.state.currentUserData}/>} />
    <Route path='/browse' render={() => <Browse items={_.values(this.state.items)}/>} />
    <Route render={function () {
      return <p>Not Found</p>
    }} />
  </Switch>
</div>
  </Router>

Home 组件中,我有一个按钮,单击该按钮会在历史记录中添加一个新网址:this.props.history.push('/?p=' + hash),其中hash 类似于f9ojy。这“有效”是指窗口中的 URL 更新为新的哈希值,但是当我单击“后退”或“前进”时,显示的页面没有变化。

【问题讨论】:

    标签: reactjs react-router react-router-v4


    【解决方案1】:

    在 Reactiflux discord 频道中找到了答案;在componentDidUpdate 我可以检查以前的this.props.location.search 并将其与当前的比较。如果不一样,我有一个基于当前参数重置 UI 的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 2018-02-10
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多