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...
You can keep all your states including navigation state in mobx store.
For example:
Then you can directly call the
dispatch
action of the store to navigate to a new screen.Yes either:
forward the navigation class to the store when calling the method:
Or you can singleton the navigator (warning only works for one ofc):
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!)