簡體   English   中英

React-router(0.13)+ Flux - 如何將flux類實例導入willTransitionTo鈎子?

[英]React-router (0.13) + Flux - how to get flux class instance into willTransitionTo hook?

我有一個容器組件,用於需要授權訪問的所有路由。 但我需要一個通用的生命周期鈎子來詢問Flux商店“用戶登錄了嗎?”。 問題是static willTransitionHook無法訪問道具(或上下文):

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>
        );
    }
}

你提出什么解決方案? 使用componentDidMount + componentDidUpdate 謝謝!

真的沒有辦法解決這個問題。 如果你想接受作為道具的flux ,你不能依賴於willTransitionTo

但是,您可以使用componentWillReceiveProps ,我相信React Router會調用它。

但是,如果您想首先禁止過渡,我不確定您應該如何繼續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM