Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

navigation.dispatch doesn't check the type of ParamList and RouteName #10816

Open
3 of 11 tasks
Yupeng-li opened this issue Sep 6, 2022 · 4 comments
Open
3 of 11 tasks

Comments

@Yupeng-li
Copy link

Yupeng-li commented Sep 6, 2022

Current behavior

When I use dispatch to send an action (such as StackActions.replace), the type is not checked against the ParamList I provided.

type ParamList = {
    Screen_Name: { id: string }
}

const AnotherScreen = ()=>{
    const navigation = useNavigation<NavigationProp<ParamList>>()
   
   // NOTE: The route name and params are not checked in the action. Errors are expected here.
    navigation.dispatch(StackActions.replace("any-string", { 'any-object': '' }))
  
   // However the type is checked when using `navigate` 
   //@ts-expect-error
   navigation.navigate('Screen_Name')
}

Expected behavior

The route name and params should be checked in the action when using dispatch function

Reproduction

https://snack.expo.dev/@yupeng_li/dispatch-type-issue

By some reasons, the example in snack doesn't show ts error properly, so I made another one in codesandbox.
https://codesandbox.io/s/dispatch-type-issue-ki72og?file=/src/App.tsx

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/​bottom-tabs
  • @react-navigation/​drawer
  • @react-navigation/​material-bottom-tabs
  • @react-navigation/​material-top-tabs
  • @react-navigation/​stack
  • @react-navigation/​native-stack

Environment

  • [] I've removed the packages that I don't use
package version
@react-navigation/native 6.0.12
@react-navigation/native-stack 6.8.0
react-native-safe-area-context 4.3.3
react-native-screens 3.17.0
react-native-gesture-handler 2.5.0
react-native 0.66.3
node 16.13.0
npm or yarn yarn 1.22.10
@Yupeng-li Yupeng-li added the bug label Sep 6, 2022
@github-actions
Copy link

github-actions bot commented Sep 6, 2022

Hey! Thanks for opening the issue. The issue doesn't seem to contain a link to a repro (a snack.expo.dev link, a www.typescriptlang.org/play link or link to a GitHub repo under your username).

Can you provide a minimal repro which demonstrates the issue? Please try to keep the repro as small as possible and make sure that we can run it without additional setup.

A repro will help us debug the issue. The issue will be closed automatically after a while if you don't provide a repro.

@amritk
Copy link

amritk commented Dec 12, 2022

Can confirm this is not working

        navigation.dispatch(
          StackActions.replace('Auth', {
            params: {
              aRandomParam: 'nope',
              verificationToken: token,
            },
            screen: 'Anything goes here',
          }),
        )

@amritk
Copy link

amritk commented Dec 13, 2022

Got it working:

export type VerificationReceivedNavProps = NativeStackNavigationProp<
  AuthStackParamList,
  'VerificationReceived'
>

const navigation = useNavigation<VerificationReceivedNavProps>()
navigation.navigate('VerificationReceived', { token })

@liamjones
Copy link

@amritk that's using navigate not dispatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants