Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应路由器(0.13)和#x2B;Flux-如何将Flux类实例放入到钩子中?_Javascript_Reactjs_Reactjs Flux_Flux_React Router - Fatal编程技术网

Javascript 反应路由器(0.13)和#x2B;Flux-如何将Flux类实例放入到钩子中?

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{

我有一个用于所有需要授权访问的路线的容器组件。但我需要一个通用生命周期钩子来询问Flux store“用户登录了吗?”。问题在于
静态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>
        );
    }
}