0
私は、routerとreduxに反応するように接続されているコンポーネントがあるとします。状態 "isLoggedIn"が偽になると、コンポーネントが自動的に別のルートにリダイレクトされるようにします。React Routerでプログラムで再ルーティングする場所はどこですか?
withRouter(connect(
state => ({
isLoggedIn: selectors.getIsLoggedIn(state),
})
)(
class AdminGate extends React.Component {
render() {
const { isLoggedIn, router, ...restProps } = this.props;
if (!isLoggedIn) {
router.push('/');
}
return isLoggedIn ? <InnerComponent {...restProps} /> : <div>Prohibited</div>;
}
}
))
現在、以下の作品が、私は、コンソールにエラーが表示されます:これは私がこれまで持っているものである
warning.js:44 Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
私はcomponentWillMount
にこのロジックを移動した場合、それは明らかに一度だけ実行されます。
これを設定する正しい方法は何ですか?私はおそらくrouter.push
を行う方法をlogOut
のように修正することができると理解していますが、それは理想的ではありません。私は状態に基づいてそれを処理するコンポーネントを好むだろう。