javascript - React Navigation navigation.navigate("SomeScreen") error in TypeScript - Stack Overflow

A mon error which occurs in react-navigation, when using TypeScript, is:Argument of type 'string&#

A mon error which occurs in react-navigation, when using TypeScript, is:

Argument of type 'string' is not assignable to parameter of type '{ key: string; params?: undefined; merge?: boolean | undefined; } | { name: never; key?: string | undefined; params: never; merge?: boolean | undefined; }'

What could this be caused by?

A mon error which occurs in react-navigation, when using TypeScript, is:

Argument of type 'string' is not assignable to parameter of type '{ key: string; params?: undefined; merge?: boolean | undefined; } | { name: never; key?: string | undefined; params: never; merge?: boolean | undefined; }'

What could this be caused by?

Share Improve this question asked Aug 12, 2022 at 3:24 Nothing hereNothing here 2,4134 gold badges22 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

This occurs when types for specific screens have not been defined.

I will describe setting up types for the stack navigation, usage in the useNavigation hook, as well as passing the {navigation} through props to a screen.

First, setup the Stack Navigator:

/**
* Types for Stack Navigator.
*/
export type StackParamList = {
  Main: undefined;
  Home: undefined;
};

const Stack = createStackNavigator<StackParamList>();

When using the useNavigation hook:

import { StackNavigationProp } from "@react-navigation/stack";

/**
 * Types for the Stack Navigator.
 */
export type StackNavigation = StackNavigationProp<StackParamList>;

const navigation = useNavigation<StackNavigation>();

When passing the navigation down as a prop in a screen:

/**
 * Types for passing the navigation props to screens in the Stack Navigator.
*/
export type StackNavigationProps = {
  navigation: StackNavigation;
};

const SomeScreenInTheStack = ({ navigation }: StackNavigationProps) => {
...
}

I extended the answer in another question, just like this, but e to find the OP is not tagging the post correctly so I created this Q&A.

Hope this is useful to someone!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744341754a4569443.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信