2016-07-26 76 views
3

嘿,我試圖在使用react-native-router-flux時獲取當前場景密鑰。react-native-router-flux,如何獲取當前的場景密鑰

我的路線文件:

const AppRouter = connect()(Router) 

class Routes extends Component { 
    render() { 
    function selector(props) { 
     console.log(props) 
     // GoogleAnalytics.trackView(props.navigationState) 
     return props.auth.isLoggedIn ? (props.auth.isVerified? 'home': 'authenticate') : 'authenticate' 
    } 

    const component = connect(state => ({ 
     auth: state.auth, 
    }))(Switch) 

    return (
     <AppRouter> 
     <Scene key="root" component={component} tabs selector={selector} hideNavBar hideTabBar> 
      <Scene key="authenticate" hideTabBar> 
      <Scene type="replace" key="login" title="Login" initial component={GradientBackground(LoginScreen)} hideNavBar/> 
      <Scene type="replace" key="register" component={GradientBackground(RegisterScreen)} hideNavBar/> 
      <Scene type="replace" key="forgotPassword" title="forgotPassword" component={GradientBackground(ForgotPasswordScreen)} hideNavBar/> 
      <Scene type="replace" key="emailConfirmation" component={GradientBackground(EmailConfirmationScreen)} hideNavBar/> 
      </Scene> 
      <Scene key="home" component={NavigationDrawer} type="replace"> 
      {require('./scenes/home')} 
      </Scene> 
     </Scene> 
     </AppRouter> 
    ) 
    } 
} 

export default Routes 

,你可以看到我在嘗試發送當前場景名稱,谷歌的跟蹤分析,但我似乎無法能夠得到當前顯示的場景的關鍵,有任何想法嗎?

回答

3

我最終實現中央減速:

const AppRouter = connect()(Router) 

class Routes extends Component { 

    render() { 
    let catchInitialState = false 

    function selector(props) { 
     return props.auth.isLoggedIn ? (props.auth.isVerified? 'home': 'authenticate') : 'authenticate' 
    } 

    const reducerCreate = params => { 
     const defaultReducer = Reducer(params) 
     return (state, action) => { 
     if(action.type == 'RootContainerInitialAction') { 
      catchInitialState = true 
     } 

     if(catchInitialState && action.type != 'RootContainerInitialAction') { 
      GoogleAnalytics.trackScreenView(action.scene.sceneKey) 
      catchInitialState = false 
     } 

     if(action.type == 'REACT_NATIVE_ROUTER_FLUX_REPLACE') { 
      GoogleAnalytics.trackScreenView(action.key) 
     } 

     return defaultReducer(state, action) 
     } 
    } 

    const component = connect(state => ({ 
     auth: state.auth, 
    }))(Switch) 

    return (
     <AppRouter createReducer={reducerCreate}> 
     <Scene key="root" component={component} tabs selector={selector} hideNavBar hideTabBar> 
      <Scene key="authenticate" hideTabBar> 
      <Scene type="replace" key="login" initial component={GradientBackground(LoginScreen)} hideNavBar/> 
      <Scene type="replace" key="register" component={GradientBackground(RegisterScreen)} hideNavBar/> 
      <Scene type="replace" key="forgotPassword" component={GradientBackground(ForgotPasswordScreen)} hideNavBar/> 
      <Scene type="replace" key="emailConfirmation" component={GradientBackground(EmailConfirmationScreen)} hideNavBar/> 
      </Scene> 
      <Scene key="home" component={NavigationDrawer} type="replace"> 
      {require('./scenes/home')} 
      </Scene> 
     </Scene> 
     </AppRouter> 
    ) 
    } 
} 

export default Routes 
+0

對於「常量defaultReducer =減速(PARAMS)」,你在哪裏導入您傳遞PARAMS的「減速」功能?我不確定它的重點是什麼:「return defaultReducer(state,action)」。 – Squeaky

+0

一旦自定義reducer做它的工作(這是幫助我跟蹤頁面更改爲谷歌分析)我將狀態和行動返回到默認reducer,所以路由器可以做到這一點魔術和更改應用程序中的視圖,否則路由器停止工作。 默認縮減器是庫的一部分: 從'react-native-router-flux'導入{Scene,Switch,Router,Reducer} – ospfranco

+0

下面是基於以下示例的示例:https://github.com/aksonov /react-native-router-flux/blob/b11a84962e4e4d9265770382b8859d21d1a12ea0/Example/Example.js – superquest