【问题标题】:React Router Confirm Navigation to a page反应路由器确认导航到页面
【发布时间】:2016-12-09 18:23:03
【问题描述】:

我正在努力学习 React。示例项目旨在与 React 0.13.3 和 react-router 0.13.3 一起使用。我正在使用 react 15.4.1 和 react-router 3.0.0

示例项目使用 willTransitionTo 在允许用户导航到页面之前进行简单确认。这是代码:

var About = React.createClass({
  statics: {
    willTransitionTo: function(transition, params, query, callback) {
      if (!confirm('Are you sure you want to read a page that\'s this boring?')) {
        transition.about();
      } else {
        callback();
      }
}, ...

我知道在我使用的 react-router 版本中,以上不再有效。因此,在找到on the react router docs page 的 auth-flow 示例之后,我将代码转换为使用 onEnter 约定。到目前为止,这是我所拥有的:

function confirmTransition(nextState, replace){
    if(comfirm('Are you sure you want to read a page that\'s this boring?')){
        replace({
            pathname: '/about',
            state: {nextPathname: nextState.location.pathname}
        });
    }
}

ReactDOM.render((
    <Router history={hashHistory}>
        <Route path="/" component={require('./components/app')}>
            <IndexRoute component={require('./components/homePage')} />
            <Route path="/authors" component={require('./components/authors/authorPage')} />
            <Route path="/about" component={require('./components/about/aboutPage')} />
            <Redirect from="about-us" to="about" />
            <Route path='*' component={require('./components/notFoundPage')} onEnter={confirmTransition}/>
        </Route>
    </Router>
), document.getElementById('app'));

我遇到的问题是当我尝试导航到 about 页面时,onEnter 没有触发。

我确定我错过了什么。我可能做错了什么?

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    好的。我……嗯,不聪明。我发现我把 onEnter 放在了错误的路线上。

    谢谢大家没有指出这一点。

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2016-05-29
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 2016-04-10
      • 2021-03-29
      相关资源
      最近更新 更多