【问题标题】:Typescript + React/Redux: Property "XXX" does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributesTypescript + React/Redux:类型'IntrinsicAttributes & IntrinsicClassAttributes 上不存在属性“XXX”
【发布时间】:2017-07-28 05:19:07
【问题描述】:

我正在使用 Typescript、React 和 Redux(都在 Electron 中运行)开展一个项目,当我在另一个中包含一个基于类的组件并尝试在它们之间传递参数时遇到了问题。粗略地说,容器组件的结构如下:

class ContainerComponent extends React.Component<any,any> {
  ..
  render() {
    const { propToPass } = this.props;
    ...
    <ChildComponent propToPass={propToPass} />
    ...
  }
}

....
export default connect(mapStateToProps, mapDispatchToProps)(ContainerComponent);

还有子组件:

interface IChildComponentProps extends React.Props<any> {
  propToPass: any
}

class ChildComponent extends React.Component<IChildComponentProps, any> {
  ...
}

....
export default connect(mapStateToProps, mapDispatchToProps)(ChildComponent);

显然,我只包括基础知识,这两个类还有更多内容,但是当我尝试运行看起来像有效代码的代码时,我仍然遇到错误。我得到的确切错误:

TS2339: Property 'propToPass' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<{}, ComponentState>> & Readonly<{ childr...'.

当我第一次遇到错误时,我以为是因为我没有传入定义我的道具的接口,但我创建了它(如您在上面看到的),但它仍然不起作用。我想知道,我有什么遗漏吗?

当我从 ContainerComponent 的代码中排除 ChildComponent 道具时,它呈现得很好(除了我的 ChildComponent 没有关键道具)但是 JSX Typescript 中的它拒绝编译它。我认为这可能与基于this article 的连接包装有关,但那篇文章中的问题发生在 index.tsx 文件中,并且是提供程序的问题,我在其他地方遇到了问题。

【问题讨论】:

    标签: reactjs typescript redux electron jsx


    【解决方案1】:

    因此,在阅读了一些相关答案(特别是 this onethis one 并查看了 @basarat 对问题的回答后,我设法找到了适合我的东西。它看起来(在我相对较新的 React 眼中)像Connect 没有为容器组件提供显式接口,因此它被它试图传递的 prop 弄糊涂了。

    所以容器组件保持不变,但子组件发生了一些变化:

    interface IChildComponentProps extends React.Props<any> {
      ... (other props needed by component)
    }
    
    interface PassedProps extends React.Props<any> {
      propToPass: any
    }
    
    class ChildComponent extends React.Component<IChildComponentProps & PassedProps, any> {
      ...
    }
    
    ....
    export default connect<{}, {}, PassedProps>(mapStateToProps, mapDispatchToProps)    (ChildComponent);
    

    以上内容对我有用。显式传递组件期望从容器中获得的道具似乎可以正常工作,并且两个组件都正确呈现。

    注意:我知道这是一个非常简单的答案,我不确定为什么会这样,所以如果一个更有经验的 React 忍者想要放弃一些关于这个答案的知识,我会很高兴修改它。

    【讨论】:

    • 但是React.Props 已经被弃用了!!
    • 在搜索了这个问题很长一段时间后,我发现解决这个问题的关键在于对connect 的最终更改 - 更新connect 的界面以包含提供的附加参数完全解决了这个问题,不需要其他更改(至少对于我的代码)。
    【解决方案2】:

    仔细检查新添加的对象类型。当对象类型不完全符合预期时,会抛出此类错误。

    Ex. 组件中提到的道具类型必须与传递给该组件的道具类型匹配。

    【讨论】:

      【解决方案3】:

      让您的子组件使用您想要的类型或“任何”类型扩展 React.Component。例如:“extends React.Component&lt;any&gt; {...}

      export class ChildComponent extends React.Component<T> {
       render() {
        return (
          <button className="square">
            {this.props.value}
          </button>
        );
       }
      }
      

      然后你可以在父组件中传递值,例如:

      renderSquare(i: Number) { return &lt;ChildComponent value={i}/&gt;; }

      查看https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/class_components/了解更多信息

      【讨论】:

        【解决方案4】:

        而不是export default connect(mapStateToProps, mapDispatchToProps)(ChildComponent);,更喜欢connect 装饰器https://github.com/alm-tools/alm/blob/00f2f94efd3810af8a80a49f968c2ebdeb955399/src/app/fileTree.tsx#L136-L146

        @connect((state: StoreState): Props => {
            return {
                filePaths: state.filePaths,
                filePathsCompleted: state.filePathsCompleted,
                rootDir: state.rootDir,
                activeProjectFilePathTruthTable: state.activeProjectFilePathTruthTable,
                fileTreeShown: state.fileTreeShown,
            };
        })
        

        这里定义了连接https://github.com/alm-tools/alm/blob/00f2f94efd3810af8a80a49f968c2ebdeb955399/src/typings/react-redux/react-redux.d.ts#L6-L36

        为什么?

        似乎您使用的定义可能已过时或无效(可能编写不当)。

        【讨论】:

        • 看起来连接子组件肯定是问题所在,但我找到了一种解决问题的方法,无需更改我正在使用的类型。使用this link 中的解决方案,我设法将连接更改为:connect&lt;{}, {}, PassedProps&gt; 其中 PassedProps 是组件从其父容器获取的道具。
        猜你喜欢
        • 2018-07-31
        • 2020-08-24
        • 2020-04-23
        • 2020-02-04
        • 2018-07-16
        • 2018-10-14
        • 2018-01-03
        • 2019-04-09
        • 1970-01-01
        相关资源
        最近更新 更多