我们应该以及如何命名反应导航屏幕道具类型?

时间:2020-03-24 18:02:04

标签: javascript reactjs typescript react-native react-navigation

根据关于类型检查的official documentation,我们必须这样命名导航道具类型:

type HomeScreenNavigationProp = StackNavigationProp<StackParamList, 'Home'>;
type HomeScreenRouteProp = RouteProp<StackParamList, 'Home'>;

interface Props {
  navigation: HomeScreenNavigationProp,
  route: HomeScreenRouteProp,
}

然后通过每个屏幕名称更改Home

但这可以很容易地简化为:

type Navigation = StackNavigationProp<StackParamList, 'Home'>;
type Route = RouteProp<StackParamList, 'Home'>;

interface Props {
  navigation: Navigation,
  route: Route,
}

或更简单的是,直接在Props界面上声明类型,如下所示:

interface Props {
  navigation: StackNavigationProp<StackParamList, 'Home'>,
  route: RouteProp<StackParamList, 'Home'>,
}

结果相同。

有什么技术上的原因可以避免我在做什么,并按照文档中的描述命名道具类型?

如果没有,是否有关于使用react-navigation和/或react-native进行类型命名的约定?

0 个答案:

没有答案