【问题标题】:Typescript doesn't understand that props are provided by react-navigation withNavigation HOCTypescript 不明白 props 是由 react-navigation withNavigation HOC 提供的
【发布时间】:2019-04-08 18:57:03
【问题描述】:

我遇到了问题

"react-navigation": "2.18.2", "@types/react-navigation": "2.13.0", "typescript": "3.1.6",

我试图在子组件中使用withNavigation HOC,但这会导致父组件说并非所有必要的道具都被传递下来。即

在父母中:

render() {
  return (
    <Child
      someProp={true}
    />
  );
}

在孩子中:

export interface IChildProps {
  someProp: boolean
}

class Child extends React.Component<IChildProps & NavigationInjectedProps> {
  render() {
    return (<Text>Child component </Text>)
  }
}

export default withNavigation(Child)

我在父级中收到此错误: Property 'navigation' is missing in type ...,表示父母似乎无法弄清楚navigation是出于某种原因通过withNavigation HOC传递给孩子的?

我当然可以从父级传递navigation 属性,但这会使withNavigation 有点无用,只是将问题移到祖先链上:笑脸:

我查看了类型定义,但找不到任何错误。有人可以帮我解决问题吗?

【问题讨论】:

    标签: typescript react-native react-navigation higher-order-functions


    【解决方案1】:

    如果我启用strictFunctionTypes 编译器选项,我可以重现该问题。 @types/react-navigation 包中的 withNavigation 声明不是为使用此选项而设计的。我已经提交了pull request 来修复它。请参阅this answer,了解在合并拉取请求之前使用我修改后的声明的可能方式。

    【讨论】:

    • 非常感谢,这很有道理! :)
    猜你喜欢
    • 2019-12-22
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2020-08-30
    相关资源
    最近更新 更多