问题
I see a lot of folks in the react-navigation issues section using this.props.navigation.dispatch
to programmatically navigate. Is there any specific reason or use case to use that over this.props.navigation.navigate
?
It seems like you can pass more options to the dispatch function? Is there anything else? Can you use dispatch without explicitly tying react-navigation into your apps redux store? Right now I have an app that has redux, but I dont explicitly configure my redux setup to know about react-navigation (and would prefer to keep it that way if I can).
回答1:
From the React Navigation docs The Navigation Prop chapter (https://reactnavigation.org/docs/navigators/navigation-prop#dispatch-Send-an-action-to-the-router):
...The other navigation functions use dispatch behind the scenes...
Also parameters for NavigationActions.navigate
and this.props.navigation.navigate
are the same. There should be no difference which one you use. In my opinion, this.props.navigation.navigate
is shorter and more readable.
回答2:
From github 👇🏻:
This is the code of navigate
function which reuses navigation.dispatch
.. And remember:
Code Don't Lie
Then, navigation.navigate(...args)
is an alias of navigation.dispatch(NavigationActions.navigate(...args))
来源:https://stackoverflow.com/questions/43869903/differences-between-this-props-navigation-dispatch-vs-this-props-navigation-navi