Javascript 打字稿&;反应导航:导航容器引用上的类型信息?
我正在使用react导航&我遇到了一个需要从组件外部导航的用例(在收到推送通知后导航) 问题是,当从组件内部使用Javascript 打字稿&;反应导航:导航容器引用上的类型信息?,javascript,typescript,react-native,react-navigation,react-navigation-v5,Javascript,Typescript,React Native,React Navigation,React Navigation V5,我正在使用react导航&我遇到了一个需要从组件外部导航的用例(在收到推送通知后导航) 问题是,当从组件内部使用navigation.navigate方法时,我会根据文档中定义的所有类型获得正确的Typescript Autocomplete和Intellisense 但是,当使用navigationRef.current?.navigate方法时,类型信息不存在 有没有办法将类型信息也带到ref对象?您可以通过向导航方法提供泛型类型来实现 假设您有一些屏幕,并且声明了路由名称param lis
navigation.navigate
方法时,我会根据文档中定义的所有类型获得正确的Typescript Autocomplete和Intellisense
但是,当使用navigationRef.current?.navigate
方法时,类型信息不存在
有没有办法将类型信息也带到ref对象?您可以通过向
导航
方法提供泛型类型来实现
假设您有一些屏幕,并且声明了路由名称param list types(),如下所示:
type RootStackParamList = {
Home: undefined;
Profile: { userId: string };
Feed: { sort: 'latest' | 'top' } | undefined;
};
// You will now be able to choose between 'Home' or 'Profile' or 'Feed' when invoking your navigate method
navigationRef.current?.navigate<keyof RootStackParamList>('Home');
为了在navigate方法中获取有关这些路由的类型信息,可以将上述声明的类型作为泛型使用,如下所示:
type RootStackParamList = {
Home: undefined;
Profile: { userId: string };
Feed: { sort: 'latest' | 'top' } | undefined;
};
// You will now be able to choose between 'Home' or 'Profile' or 'Feed' when invoking your navigate method
navigationRef.current?.navigate<keyof RootStackParamList>('Home');
//在调用导航方法时,您现在可以在“Home”或“Profile”或“Feed”之间进行选择
navigationRef.current?导航(“主页”);