我遇到了react-router问题。React路由器取决于React组件的状态
我的路线文件如下:
const routes = (
<Route path="/" component={App}>
<IndexRoute component={HomePage}/>
<Route path="registration" component={Registration}>
<Route path="/registration-step-one" component={RegistrationStepOne} />
<Route path="/registration-step-two" component={RegistrationStepTwo}/>
<Route path="/registration-step-three" component={RegistrationStepThree}/>
<Route path="/registration-step-four" component={RegistrationStepFour}/>
</Route>
<Route path="reset-password" component={PasswordReset} />
</Route>
);
我有一个很大的组成部分注册其中有四个步骤。通过点击提交按钮,状态改变,下一步显示。
注册组件代码:
onButtonClick(name, event) {
event.preventDefault();
switch (name) {
case "stepFourConfirmation":
this.setState({step: 1});
break;
case "stepTwoNext":
this.setState({step: 3, errors: {}});
break;
case "stepThreeFinish":
this.setState({step: 4});
break;
default:
this.setState({step: 2, errors: {}});
}
}
render() {
const languageReg = this.props.currentLanguage.default.registrationPage;
let formStep = '';
let step = this.state.step;
switch (step) {
case 1:
formStep = (<RegistrationFormStepOne user={this.state.user}
onChange={this.setUser}
onButtonClick={this.onButtonClick}
onClick={this.changeStep}
errors={this.state.errors}/>);
break;
case 2:
formStep = (<RegistrationFormStepTwo user={this.state.user}
onChange={this.setUser}
onButtonClick={this.onButtonClick}
onClick={this.changeStep}
errors={this.state.errors}/>);
break;
case 3:
formStep = (<RegistrationFormStepThree user={this.state.user}
onFileChange={this.onFileChange}
onButtonClick={this.onButtonClick}
onClick={this.changeStep}
errors={this.state.errors}
files={this.state.files}
fileChosen={this.state.selectedFile}/>);
break;
default:
formStep = (<RegistrationFormStepFour user={this.state.user}
onChange={this.setUser}
onChangeCheckboxState={this.changeCheckboxState}
emailNotificationsState={this.state.user.emailNotifications}
termsState={this.state.user.terms}
smsNotificationsState={this.state.user.smsNotifications}
onButtonClick={this.onButtonClick}
onClick={this.changeStep}
errors={this.state.errors}/>);
}
return (
<div className="sidebar-menu-container" id="sidebar-menu-container">
<div className="sidebar-menu-push">
<div className="sidebar-menu-overlay"></div>
<div className="sidebar-menu-inner">
<div className="contact-form">
<div className="container">
<div className="row">
<div className="col-md-10 col-md-offset-1 col-md-offset-right-1">
{React.cloneElement(formStep, {currentLanguage: languageReg})}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
但是,如果我在一个例如第三步,我点击返回浏览器我重定向到主页。有没有什么办法可以让路由器只进入前一步ant而不是主页?或者,如果我使用状态来更改表单步骤,则无法完成此操作?
我得到_TypeError:无法读取属性'setRouteLeaveHook'undefined_ – Boky
尝试'this.context.router .....'而不是。所以用'context'替换'props'。 – Chris
我已添加'RegistrationPage.contextTypes = { router:PropTypes.object };'使上下文可用。但现在我得到_Uncaught TypeError:无法读取undefined_的属性'step'。 – Boky