小编Mel*_*let的帖子

为反应导航道具添加强类型

我在我的react-native项目(expo)中使用typescript.

该项目使用反应导航,所以在我的屏幕上我可以设置navigationOptions并且我可以访问道具navigation.

现在我正在尝试强类型这些,所以我得到了可用于设置的属性的提示.

interface NavStateParams {
    someValue: string
}

interface Props extends NavigationScreenProps<NavStateParams> {
   color: string
}

class Screen extends React.Component<Props, any> {
    // This works fine
    static navigationOptions: NavigationStackScreenOptions = {
        title: 'ScreenTitle'
    }
    // Does not work
    static navigationOptions: NavigationStackScreenOptions = ({navigation, screenProps }) => ({
        title: navigation.state.params.someValue
    })
}
Run Code Online (Sandbox Code Playgroud)

什么是处理反应导航作为组件道具的最佳方法.

typescript react-native react-navigation

23
推荐指数
4
解决办法
1万
查看次数