对象的属性是未定义的对象:React.PropTypes.object
问题描述:
我试图让我的第一个真正的反应和REDX应用程序的身份验证和运行,但我坚持管理路线访问和验证。对象的属性是未定义的对象:React.PropTypes.object
这里是我的代码来验证授权:
export default function(ComposedComponent) {
class Authentication extends Component {
static contextTypes = {
router: React.PropTypes.object
}
componentWillMount() {
if (!this.props.authenticated) {
this.context.router.push('/');
}
}
componentWillUpdate(nextProps) {
if (!nextProps.authenticated) {
this.context.router.push('/');
}
}
render() {
return <ComposedComponent {...this.props} />
}
}
function mapStateToProps(state) {
return { authenticated: state.auth.authenticated };
}
return connect(mapStateToProps)(Authentication);
}
我进口,并要求它在我的路线上的位置:
class App extends Component {
render() {
return (
<Router history={history}>
<Layout>
<Route path='/' exact component={Home} />
<Route path='/signin' component={SignIn} />
<Route path='/signout' component={SignOut} />
<Route path='/brain' component={RequireAuth(Brain)} />
</Layout>
</Router>
);
}
}
export default App;
这是我收到的错误:
TypeError: Cannot read property 'object' of undefined
./src/components/auth/RequireAuth.js.__webpack_exports__.a
src/components/auth/RequireAuth.js:7
4 | export default function(ComposedComponent) {
5 | class Authentication extends Component {
6 | static contextTypes = {
> 7 | router: React.PropTypes.object
8 | }
9 |
10 | componentWillMount() {
View compiled
感谢您提供的任何方向!
答
你可以试试吗?希望它有帮助
static contextTypes = {
router: React.PropTypes.func
}
+0
试过了,但没有这样的运气!谢谢你尝试,但我很感激。 –
您运行的是哪个版本的React? 'React.PropTypes'从v15.5开始已被弃用。它现在是它自己的'PropTypes'模块(https://www.npmjs.com/package/prop-types)。你可能应该导入它,然后使用'PropTypes.object'。 – SamVK
这固定它,非常感谢你。是的,我使用最新版本的React,并使用了一个有点过时的教程,所以我认为这是一个问题,但在搜索时找不到任何东西。再次感谢! –