Javascript 反应路由器(0.13)和#x2B;Flux-如何将Flux类实例放入到钩子中?
我有一个用于所有需要授权访问的路线的容器组件。但我需要一个通用生命周期钩子来询问Flux store“用户登录了吗?”。问题在于Javascript 反应路由器(0.13)和#x2B;Flux-如何将Flux类实例放入到钩子中?,javascript,reactjs,reactjs-flux,flux,react-router,Javascript,Reactjs,Reactjs Flux,Flux,React Router,我有一个用于所有需要授权访问的路线的容器组件。但我需要一个通用生命周期钩子来询问Flux store“用户登录了吗?”。问题在于静态willTransitionHook无法访问道具(或上下文): 类AuthenticatedHandler扩展React.Component{ 静态转换(转换){ //'this.props.flux'不可访问 } componentDidMount(){ console.log('did mount',this.props); } render(){ const{
静态willTransitionHook
无法访问道具(或上下文):
类AuthenticatedHandler扩展React.Component{
静态转换(转换){
//'this.props.flux'不可访问
}
componentDidMount(){
console.log('did mount',this.props);
}
render(){
const{flux}=this.props;
返回(
({
isLoggedIn:store.isLoggedIn(),
用户:store.getUser()
})
}}>
);
}
}
你提出了什么解决方案?使用
componentDidMount
+componentdiddupdate
?谢谢 真的没有办法。如果您希望以道具形式接收flux
,则不能依赖将转换为
但是,您可以使用组件willreceiveprops
,我相信它是由React路由器调用的
不过,如果你想一开始就不允许过渡,我不确定你应该如何继续
class AuthenticatedHandler extends React.Component {
static willTransitionTo(transition) {
// `this.props.flux` is not accessible
}
componentDidMount() {
console.log('did mount', this.props);
}
render() {
const { flux } = this.props;
return (
<FluxComponent flux={flux} connectToStores={{
user: store => ({
isLoggedIn: store.isLoggedIn(),
user: store.getUser()
})
}}>
<RouteHandler />
</FluxComponent>
);
}
}