假设我有一个组件<MyComponent check={this.props} />
。这将调用MyComponent
类。在这里,我做这样的事情:反应如何在传递另一个组件前检查道具
render(){
const {user} = this.props
return(){
<div>Welcome {user.name}</div>
}
}
在我的容器最初没有user.name
当用户在那么只有user.name
是道具可登录。在componentWillMount
我检查了isLoggedIn
,如果没有,我重定向它,但在render
方法它检查登录前不可用的属性user.name
。
如果isLoggedIn
为真,那么<MyComponent check={this.props} />
其中调用我的MyComponent
应返回重定向或其他。
有没有人有任何想法如何实现这一点?
你试过'componentWillReceiveProps(nextProps)'方法吗? –