如何使用反应导航在mobx存储中进行导航? [英] How to navigate in mobx store using react navigation?
问题描述
我可以使用屏幕组件中的this.props.navigation
进行导航.我应该如何在mobx存储文件中执行类似操作?还是应该在商店中进行导航?
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?
有人说使用global
变量存储this.props.navigation
引用并在任何地方使用,但是我不喜欢这个主意...
Someone says use global
variable to store a this.props.navigation
reference and use it anywhere, but I don't like the idea...
推荐答案
是:
在调用方法时将导航类转发给商店:
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} />;
呈现后,它将在商店中设置navigator属性.
after this is rendered it will set the navigator property in the store.
但是我建议您将所有路由状态也存储在这样的路由存储中: https ://github.com/alisd23/mobx-react-router .
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!)
这篇关于如何使用反应导航在mobx存储中进行导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!