2017-01-07 5 views
1

1つのパスに対して2つのコンポーネントを持つ1つのアプリケーションを作成しています。私はAppContainerコンポーネントを使用して私のアプリの中に入っています。この場合authの場合は、リアクタルータの複数コンポーネントonEnter?

<Router history={hashHistory}> 
    <Route path="/" component={AppContainer} onEnter={requireEnter}> 
     <Route path="/homepage" component={HomePage} /> 
     <Route component={MainPage} onEnter={requireAuth}> 
      <Route path="/home" component={DashBoard} /> 
     </Route> 
    </Route> 
</Router> 

:ここに私のルートです。その代わりに、私はpath="/"を持つホームページを持っているので、そのコンポーネントは私の選択でなければなりません。

function requireEnter (nextState, replace) { 
    if (nextState.location.pathname == "/") { 
     if (checkLoggedIn()) { 
      replace({ 
       pathname: '/home' 
      }) 
     } else { 
      replace({ 
       pathname: '/homepage' 
      }) 
     } 
    } 
} 

しかし、私はこのような何かことをしたい:今

onEnter={requireEnter}は、次の場合に処理しているあなたはそれでいないコンポーネントにルートを追加することができます

function requireEnter (nextState, replace) { 
    if (nextState.location.pathname == "/") { 
     if (checkLoggedIn()) { 
      //component should be AppContainer and redirect to '/home' 
     } else { 
      //component should be home page 
     } 
    } 
} 

答えて

0

は、それを使用していました別の同様の場合:

<Route path="/" onEnter={requireEnter}> 
    <Route path="home" component={AppContainer}> 
     <IndexRoute component={HomeContainer} /> 
    </Route> 
    <Route path="homepage" component{HomePageContainer}> 
    </Route> 
</Route> 

次に通常通り:

function requireEnter (nextState, replace){ 
    if(nextState.location.pathname=="/"){ 
     if(checkLoggedIn()){ 
     replace({ 
       pathname: '/home' 
      }); 
     }else{ 
     replace({ 
       pathname: '/homepage' 
     }); 
     } 
    } 
} 
関連する問題