question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-navigation] unable to use typing for navigation.state.params

See original GitHub issue

In 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:open
  • Created 5 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
Ashoatcommented, Nov 10, 2018

It’s admittedly non-obvious how to type a custom navigation route. Here’s how it’s done:

type NavProp = NavigationScreenProp<{|
  ...NavigationLeafRoute,
  params: {|
    color: string,
    threadInfo: ThreadInfo,
    setColor: (color: string) => void,
  |},
|}>;

Also it would be nice, if navigation.setParams() will have $Shape definition so it can autocomplete and typecheck passed object

PRs welcome! Make sure to send them to the react-navigation repo, though. (Give this CONTRIBUTING doc a read for context.)

0reactions
schumanndcommented, Aug 15, 2019

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

type Props = {
  navigation: { state: {params: {par1: string, par2: number}}},
}

export default class ComponentA extends React.Component<Props> {
  render() {
    console.log(this.props.navigation.state.params.par1) // works
    console.log(this.props.navigation.state.params.par3) // throws flow error
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found