小编Chr*_*ado的帖子

React Native with typescript - 如何使用来自@react-navigation/native 的 useRoute with typescript

我正在尝试从中获取我的事件对象,route.params但我不知道如何让打字稿识别此道具。

这是导航到我的详细信息页面的函数,将事件传递给参数:

const navigateToDetail = (incident: IncidentProps): void => {
    navigation.navigate('Detail', { incident });
  };
Run Code Online (Sandbox Code Playgroud)

这是我尝试从 route.params 获取此对象的详细信息页面代码的一部分:

type IncidentRouteParams = {
  incident: IncidentProps;
}

const Detail: React.FC = () => {
  const navigation = useNavigation();
  const route = useRoute();

  const incident = route.params.incident;
Run Code Online (Sandbox Code Playgroud)

我想我需要以某种方式将这个IncidentRouteParams类型传递给const route = useRoute()

提前致谢。

这是带有错误的图像:

编辑:

我确实喜欢这个,并且它有效,但我不知道它是否是正确的方法:

  const route = useRoute<RouteProp<Record<string, IncidentRouteParams>, string>>();

  const incident = route.params.incident;
Run Code Online (Sandbox Code Playgroud)

typescript react-native react-navigation

15
推荐指数
2
解决办法
1万
查看次数