【问题标题】:React router browser back button isn't workingReact路由器浏览器后退按钮不起作用
【发布时间】:2016-07-01 06:28:56
【问题描述】:

我正在尝试使用 React JS 和 react-router 创建一个多步骤表单。

表单步骤随状态而变化。如果我单击下一步按钮,状态步将递增并显示下一步。

但是这样,如果我在例如第三步并且我在浏览器中单击背面,我将被重定向到主页而不是上一步。

我的主要组件是这样的:

componentWillMount(){
    console.log(this.props.params.id);
}

onButtonClick(name, event) {
    event.preventDefault();

    switch (name) {
        case "stepFourConfirmation":
            if(this.validation("four")) {
                this.props.actions.userRegistrationThunk(this.state.user);
                this.setState({step: 5, user: {}});
            }
            break;
        case "stepTwoNext":
            if(this.validation("two")) {
                this.setState({step: 3});
                this.context.router.push("stepThree");
            }
            break;
        case "stepThreeFinish":
            this.setState({step: 4});
            break;
        default:
            if(this.validation("one")) {
                this.setState({step: 2});
                this.context.router.push('stepTwo');
            }
    }
}

在每次单击按钮时,我都会将参数推送到 url 并更改步骤。当我单击下一步时,它工作完美。在 componentWillMount 中,我试图从 url 获取参数,然后我会根据参数减少步骤。

但只有在加载第一步时,我才会在控制台中设置 stepOne。如果我点击下一步,我会得到 [react-router] Location "stepTwo" did not match any routes

我的 routes.js 文件如下:

    import React from 'react';
import {IndexRoute, Route} from 'react-router';

import App from './components/App';
import HomePage from './components/HomePage';
import Registration from './components/registration/RegistrationPage';

import UserPage from './components/user/userHome';
import requireAuth from './common/highOrderComponents/requireAuth';
import hideIfLoggedIn from './common/highOrderComponents/hideIfLoggedIn';

import PasswordReset from './common/passwordReset';

const routes = (
    <Route path="/" component={App}>
        <IndexRoute component={HomePage}/>
        <Route path="registration/:id" component={hideIfLoggedIn(Registration)}/>
        <Route path="reset-password" component={PasswordReset} />
        <Route path="portal" component={requireAuth(UserPage)} />
    </Route>
);

export default routes;

有什么解决办法吗?

【问题讨论】:

  • 你能提供一个要点
  • 我所有的代码?每个表单步骤都有组件?
  • 至少是主要组件。更多相关信息更好
  • 这里是link
  • 你试过browserHistory.push(&lt;your-route&gt;)吗?

标签: javascript reactjs react-router


【解决方案1】:

尝试推送绝对路径。而不是

this.context.router.push("stepTwo");

试试

this.context.router.push("/registration/stepTwo");

此外,您的生命周期方法存在问题。 componentWillMount 只被调用一次。当您推送下一条路由时,组件不会再次挂载,因此您看不到带有下一个参数的日志。初始日志使用componentWillMount,下一个日志使用componentWillReceiveProps:

componentWillMount() {
  console.log(this.props.params.id);
}

componentWillReceiveProps(nextProps) {
  console.log(nextProps.params.id);
}

顺便说一句:这是discussion on github。创建者声明,react 路由器不支持相对路径

【讨论】:

  • 当我推送 /registration/stepTwo 时,我没有收到任何 react-router 错误,但控制台日志中的参数仍然是 stepOne,但它需要是 stepTwo。
  • 编辑了我的答案,包括正确的生命周期方法
  • 太棒了。有效。但是我现在在第二步刷新页面时遇到了问题。我得到 external.js:1 Uncaught SyntaxError: Unexpected token
  • 这很可能与此问题无关。也许您的组件中有一些语法错误,例如在第二步的 jsx 中编写
  • 这不仅适用于第二步。无论我在哪一步刷新页面,我都会收到此错误。当我第一次加载第一步时,我看到一切都很好,但是当我刷新页面时,我得到了那个错误。
猜你喜欢
  • 2016-06-09
  • 2011-10-18
  • 1970-01-01
  • 2013-01-20
  • 2012-05-10
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多