【问题标题】:Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>''Readonly<{ children?: ReactNode; 类型上不存在属性 'XYZ' }> & 只读<{}>'
【发布时间】:2019-02-14 09:24:57
【问题描述】:

尝试访问 RecipeList.js 和 Recipe.js 的 .props 时出现语法错误。

这里是 Recipe.js 的代码示例:

import React, {Component} from 'react';
import "./Recipe.css";

class Recipe extends Component {

    // props: any; uncommenting this will fix the bug

    render() {
        // don't have to use return and parentheses for arrow with JSX
        const ingredients = this.props.ingredients.map((ing, ind) => (
            <li key={ind}>{ing}</li>
        ));
        const {title, img, instructions} = this.props

        return (
            <div className="recipe-card">
                <div className="recipe-card-img">
                    <img src={img} alt={title}/>
                </div>
                <div className="recipe-card-content">
                    <h3 className="recipe-title">
                        {title}
                    </h3>
                    <h4>
                        Ingredients:
                    </h4>
                    <ul>
                        {ingredients}
                    </ul>
                    <h4>
                        Instructions:
                    </h4>
                    <p>
                        {instructions}
                    </p>
                </div>
            </div>
        )
    }
}

Screenshot of .props error

但是,该项目没有引发编译时错误,并且网站运行良好。

Screenshot of app working fine with no chrome console or terminal errors

我认为这与我的代码关系不大,而与 TypeScript 或某种带有 Javascript 的 VScode 预设配置的关系更是如此,因为我在构建 @ 时遇到了类似的问题,因此无法识别每个组件的 .props 属性987654323@ 到我的编辑器中(我什至从网站复制粘贴了最终的 index.js 代码以确保),尽管应用程序运行良好,没有编译时错误。

Screenshot of the same .prop errors after following React Tutorial

解决这个问题的唯一方法是,如果我真的硬编码并为每个类创建一个 props 属性并将其设置为任何类似的属性:

Screenshot of only solution to the syntax error

这是我更新的依赖项

"dependencies": {
  "@types/react": "^16.4.13",
  "prop-types": "^15.6.2",
  "react": "^16.5.0",
  "react-dom": "^16.5.0",
  "react-scripts": "1.1.5",
  "typescript": "^3.0.3"
 }

【问题讨论】:

标签: javascript reactjs typescript visual-studio-code babeljs


【解决方案1】:

你也可以用

解决这个问题
class Recipe extends React.Component<any, any>{

....
....

// The rest of your normal code
}

【讨论】:

    【解决方案2】:

    基于 Klugjos 的回答。你可以对 React 的功能组件 (FC) 做同样的事情,并使用 useState Hook 来管理状态。

    import React, {FC} from 'react';
    import "./Recipe.css";
    
    interface IRecipeProps {
      ingredients?: string[];
      title?: string;
      img?: string;
      instructions?: string;
    }
    
    interface IRecipeState {
    }
    
    const Recipe:FC<IRecipeProps> = (props) => {
    
        const { ingredients, title, img, instructions} = props;
    
        ingredients.map(( ingredient, index) => (
            <li key={index}>
              { ingredient}
            </li>
        ));
    
        return (
            <div className="recipe-card">
                Your render code here
            </div>
        )
    }
    

    【讨论】:

      【解决方案3】:

      你需要使用 interface 和 TypeScript 的 React.Component 通用实现来定义你的 props 和 state 的样子

      import React, {Component} from 'react';
      import "./Recipe.css";
      
      interface IRecipeProps {
        ingredients?: string[];
        title?: string;
        img?: string;
        instructions?: string;
      }
      
      interface IRecipeState {
      }
      
      class Recipe extends Component<IRecipeProps, IRecipeState> {
          render() {
              const ingredients = this.props.ingredients.map((ing, ind) => (
                  <li key={ind}>{ing}</li>
              ));
              const {title, img, instructions} = this.props
      
              return (
                  <div className="recipe-card">
                      Your render code here
                  </div>
              )
          }
      }
      
      • 我会将文件扩展名更改为 .tsx 以表明它是使用 TypeScript 的 React 文件 -> Recipe.tsx
      • 请调整类型(字符串)以适合您的数据。
      • 使用IRecipeState 定义组件状态的结构(this.state.fooBar)。现在可以将其留空,因为您不使用状态。
      • 确保对引发错误的其他组件执行相同操作 (RecipeList.js)

      【讨论】:

        猜你喜欢
        • 2020-11-07
        • 2020-01-24
        • 2019-12-11
        • 2020-11-29
        • 2018-10-11
        • 2018-10-14
        • 2020-09-03
        • 1970-01-01
        • 2021-09-06
        相关资源
        最近更新 更多