【发布时间】:2017-09-08 04:40:19
【问题描述】:
单击提交按钮后,我尝试重定向我的页面,为此,我的 onSubmit 方法如下所示;
onSubmit(e){
e.preventDefault();
if (this.isValid()){
this.setState({ errors: {}, isLoading: true });
this.props.userSignupRequest(this.state).then(
() => {
this.context.router.push('/');
},
( data ) => this.setState({ errors: data.response.data, isLoading: false })
);
}
}
还有我的 propTypes 和 contextTypes;
SignupForm.propTypes = {
userSignupRequest: React.PropTypes.func.isRequired
}
SignupForm.contextTypes = {
router: React.PropTypes.object.isRequired
}
我不知道为什么,但它一直显示错误并且没有重定向。 我也试过 browserHistory.push('/') 也没有用。
我犯的错误是;
Uncaught (in promise) TypeError: _this2.context.router.push is not a function
at props.userSignupRequest.then._this2.setState.errors
正如我所说,我也尝试过 browserHistory 之类的;
import { browserHistory } from 'react-router';
和onSubmit方法;
onSubmit(e){
e.preventDefault();
if (this.isValid()){
this.setState({ errors: {}, isLoading: true });
this.props.userSignupRequest(this.state).then(
() => {
browserHistory.push('/');
},
( data ) => this.setState({ errors: data.response.data, isLoading: false })
);
}
}
我的路由器也是这样的;
const router = (<Provider store={store}>
<Router history={browserHistory}>{routes}</Router>
</Provider>);
它抛出的错误与另一个相同。
Uncaught (in promise) TypeError: Cannot read property 'push' of undefined
at props.userSignupRequest.then._this2.setState.errors
如果还需要什么,请在下面添加,我会编辑我的帖子。
谢谢。
【问题讨论】:
-
你使用什么版本的 react 和 react-router
-
反应:15.5.3 反应路由器:4.0.0 @ShubhamKhatri
-
对于 react 15.5.3 React.Proptypes is deprecated 。安装
prop-types并像import PropTypes from 'prop-types'一样导入它并使用router: PropTypes.object.isRequired。让我知道这是否有帮助 -
我已经在 contextTypes 的上方声明了 proptypes。没有写在我的帖子上,但如果你说,如果我声明了 propTypes 并且我已经需要安装 prop-types 也没关系,那么我会试一试。 (我编辑了我的帖子并添加了 propTypes)
标签: javascript reactjs redirect react-router