2016-03-22 21 views
5

反応ルータ(Reduxと組み合わせて)で初期ルートを設定する方法について質問があります。いくつかのルートが設定されており、Reduxストアの状態に基づいて、ページロード時にユーザーを常に特定のルートにリダイレクトする必要があります。反応ルータで初期ルートを設定する

次のように私のルートは、現在設定されている方法は次のとおりです。

<Provider store={store}> 
    <Router history={history}> 
     <Route path="/" component={App} onEnter={redirectToInitialRoute}> 
      <Route path="/send" component={OverviewPage} /> 
      <Route path="/thanks" component={ConfirmPage} /> 
      <Route path="/:categoryIdentifier" component={CategoryPage} /> 
     </Route> 
    </Router> 
</Provider> 

私は私のルートルートにフォーカス取得時の機能を追加しました。私はいつでもページロードにリダイレクトする必要があるので、ユーザーがアプリケーションに入力しているページとは関係なく、これを行っています。次のように私のフォーカス取得時の機能が設定され方法がある:

function redirectToInitialRoute (nextState, replace) { 
    if (condition) { 
     replace('/send'); 
    } 
    else if (anotherCondition) { 
     replace('/thanks'); 
    } 
} 

ただし、この設定で何が起こるか、(例えば)「anotherCondition」は満たされていることであると「/ありがとう」にリダイレクトします。 onEnterはルートルートを渡されるので、redirectToInitialRouteが再びトリガされます。 'anotherCondition'がまだ真であるため、リダイレクトが再度発生し、リダイレクトループが発生します。

この問題を解決するにはどうすればよいのでしょうか?どんな助けでも大歓迎です。前もって感謝します!

答えて

5

インデックスルートを追加し、マウント時にリダイレクトするのはどうですか?

componentDidMount: function() { 
    if (condition) { 
     browserHistory.push('/here'); 
    } else { 
     browserHistory.push('/there'); 
    } 
} 
:あなたの歓迎コンポーネントで次に

<Provider store={store}> 
    <Router history={history}> 
     <Route path="/" component={App} onEnter={redirectToInitialRoute}> 
      <IndexRoute component={Welcome} />   
      <Route path="/send" component={OverviewPage} /> 
      <Route path="/thanks" component={ConfirmPage} /> 
      <Route path="/:categoryIdentifier" component={CategoryPage} /> 
     </Route> 
    </Router> 
</Provider> 

関連する問題