[react-navigation] unable to use typing for navigation.state.params
See original GitHub issueIn react-navigation_v2.x.x
flow types we have type
type NavigationParams = {
[key: string]: mixed,
}
So if i define my navigation prop as
navigation: NavigationScreenProp<NavigationStateRoute>
it assumes that params are of mixed type, but i need exact, like:
type Props = {
navigation: NavigationScreenProp<NavigationStateRoute<{
foo: number,
bar: string
}>>`
}
Also it would be nice, if navigation.setParams()
will have $Shape definition so it can autocomplete and typecheck passed object
So the main problem is, can we somehow tell flow about navigation state params type?
cc @Ashoat
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Type error in getting route params within nested navigator ...
Solution using separate types for navigation and route. We can type the navigation object and the route object separately as follows.
Read more >Navigating without the navigation prop
You need to navigate from inside a component without needing to pass the navigation prop down, see useNavigation instead. · You use a...
Read more >Validating react navigation with Type Script Generics - Medium
In the Details screen only HomeNavigationParams parameters are available to get (this.props.navigation.state.params.name). In old javascript code if you missed ...
Read more >Getting Started with React Navigation v6 and TypeScript in ...
If you are using @react-navigation/stack , you can use StackScreenProps instead of StackNavigationProp . Adding type checks for route params. To ...
Read more >React Native Tutorial 70 - Passing params between screens ...
React Native Tutorial 70 - Passing params between screens ( React Navigation ). Watch later. Share. Copy link. Info. Shopping. Tap to unmute....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s admittedly non-obvious how to type a custom navigation route. Here’s how it’s done:
PRs welcome! Make sure to send them to the react-navigation repo, though. (Give this
CONTRIBUTING
doc a read for context.)Maybe I understand these examples wrong, but they are not working for me.
What I am looking for is a way to define types for
navigation.state.params
that a Component expects.So that flow will notice and error: a) if the component A tries to access params, that are not supposed to be there. b) if another component B navigates to component A, and does not provide the expected params.
A way that only solves case a) is
However, if I tried to navigate to this component from another one like so
this.props.navigation.navigate('ComponentA', {par1: string});
It does not error, even though
par2
is missing.So case b) might not be possible to do with flow, as the connection through
react-navigation
might not be easy to extract.