繁体   English   中英

类型中缺少 TypeScript 属性“导航”,但类型“道具”React Native 中需要

[英]TypeScript Property 'navigation' is missing in type but required in type 'Props' React Native

在 React Native 应用程序中,我知道当您在 createAppContainer 中传递您的 RootNavigator 并将其导出时,导航prop在所有子组件中隐式可用。

export default createAppContainer(AppNavigator);

我有这个组件应该使用导航道具导航到另一个屏幕。 我为这个组件的 props 定义了一个接口,如下所示:

ComponentA 的 props 接口:

interface Props {
  count: number;
  navigation: NavigationStackProp<{quizId: number}>;
}

现在我像这样渲染 ComponentA:

...
render() {
    return (
        <ComponentA count={0} /> // TypeScript screams here that I am not passing the navigation props
    );
}

如果我像这样通过 ComponentA 传递导航道具:

<ComponentA count={0} navigation={props.navigation} />

TypeScript 很满意。

因此,我的问题是,我是否必须像这样明确地传递导航道具? 对我来说感觉不对。 有没有办法可以在这种特殊情况下抑制 TypeScript 警告。

我是 TypeScript 的新手,所以这可能是微不足道的,但我会感谢任何帮助。

谢谢

您可以通过使用可选道具?

interface Props {
  count: number;
  navigation?: NavigationStackProp<{quizId: number}>;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM