How to navigate in mobx store using react navigation?

后端 未结 3 1494
甜味超标
甜味超标 2021-01-16 11:36

I can use this.props.navigation from screen component to navigate. How should I do the similar in mobx store file? Or should I perform navigation in store?

相关标签:
3条回答
  • 2021-01-16 12:02

    You can keep all your states including navigation state in mobx store.

    For example:

    // sourced and modified from https://github.com/react-community/react-navigation/issues/34#issuecomment-281651328
    
    class NavigationStore {
      @observable headerTitle = "Index"
      @observable.ref navigationState = {
        index: 0,
        routes: [
          { key: "Index", routeName: "Index" },
        ],
      }
    
      // NOTE: the second param, is to avoid stacking and reset the nav state
      @action dispatch = (action, stackNavState = true) => {
        const previousNavState = stackNavState ? this.navigationState : null;
        return this.navigationState = AppNavigator
            .router
            .getStateForAction(action, previousNavState);
      }
    }
    
    // NOTE: the top level component must be a reactive component
    @observer
    class App extends React.Component {
      constructor(props, context) {
        super(props, context)
        // initialize the navigation store
        this.store = new NavigationStore()
      }
    
      render() {
        // patch over the navigation property with the new dispatch and mobx observed state
        return (
          <AppNavigator navigation={addNavigationHelpers({
            dispatch: this.store.dispatch,
            state: this.store.navigationState,
            addListener: () => { /* left blank */ }
          })}/>
        )
      }
    };
    

    Then you can directly call the dispatch action of the store to navigate to a new screen.

    0 讨论(0)
  • 2021-01-16 12:02

    Send this one this.props.navigation as a parameter to the store. Then use as you use on the component side.

    LoginStore.login(this.props.navigation)
    

    in the LoginStore

    @action login = (navigation) => { navigation.navigate('Page');}
    
    0 讨论(0)
  • 2021-01-16 12:17

    Yes either:

    forward the navigation class to the store when calling the method:

    // add nivagation parameter to store fucntion:
    this.props.store.handleSomething(data, this.props.navigation);
    

    Or you can singleton the navigator (warning only works for one ofc):

    return <Navigator ref={(nav) => this.props.store.navigator = nav} />;
    

    after this is rendered it will set the navigator property in the store.

    But I would suggest to store all your routing state also in a routing store like this: https://github.com/alisd23/mobx-react-router.

    This way you always have easy access to the navigation state and you can be sure everything properly re-renders. (when in render function in components also depends on navigation changes!)

    0 讨论(0)
提交回复
热议问题