2017-08-06 24 views
2

我想做一個有條件的HomeScreen,如果用戶有一個「用戶ID」保存到手機,它應該帶他們到「PlacesScreen」,否則它會加載「 LoginScreen」。我在Redux中使用StackNavigation,大約兩週時間學習react-native。StackNavigation反應本地博覽會有條件HomeScreen

function nav (state = firstState(), action) { 
    let nextState; 
    switch (action.type) { 

    case 'Home': 
     nextState = AppNavigator.router.getStateForAction(
     NavigationActions.navigate({routeName: 'Home'}), 
     state 
    ); 
     return nextState; 
     break; 

    case NavActionTypes.NAVIGATE_PLACES: 
     nextState = AppNavigator.router.getStateForAction(
     NavigationActions.navigate({ routeName: 'Places' }), 
     state 
     ); 
     return nextState; 
     break; 
} 

...等 我想設置的減速機的初始狀態是依賴於天氣或不是一個ID保存在手機上。我是非常新的反應 - 原生和編碼一般,即時19,所以任何幫助將不勝感激!如果您需要更多代碼來幫助,請告訴我,我將發送github鏈接。 提前謝謝! Cooper

回答

1

由於redux建議讓reducer保持純淨,所以我將導航邏輯從reducer中移出並放入組件。 另外,如果狀態包含用戶標識,那麼我會建議在組件安裝期間檢查LoginScreen上的狀態並將導航狀態重置爲PlacesScreen。

例如

import store from '../storeLocation' 
export class LoginScreen extends React.Component { 

    ... 

    componentWillMount(){ 
    if(store.getState().userID !== "") 
     this.props.navigation.dispatch(NavigationActions.reset({ 
     index: 0, 
     actions: [ 
      NavigationActions.navigate({ routeName: 'Places'}) 
     ] 
     })); 
    } 

    ... 

} 
+0

謝謝你這個作品! –