How to navigate in mobx store using react navigation?

ⅰ亾dé卋堺 提交于 2019-12-01 11:38:38

问题


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?

I read the Navigating without the navigation prop article, but it seems only works for screen components, right?

Someone says use global variable to store a this.props.navigation reference and use it anywhere, but I don't like the idea...


回答1:


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!)




回答2:


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.



来源:https://stackoverflow.com/questions/49767879/how-to-navigate-in-mobx-store-using-react-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!