类型'Readonly <{children?:ReactNode;不存在属性'XYZ'。}>和只读<{}>'

Loo*_*Boi 8 javascript babel typescript reactjs visual-studio-code

修正:我在全球范围内卸载了VScode,打字稿和eslint。一旦我重新安装了VScode,一切都会恢复正常。

更新:安装@ types / react库后,我仍然仅对RecipeList.js和Recipe.js都得到类似的错误,属性“ props”中提到的在 类型“ Home”上不存在TypeScript属性“ props”不存在

属性'recipes'在类型'Readonly <{children?:ReactNode;上不存在。}>和只读<{}>'。

![安装@ types / react库后出现类似错误

原文:所以我是React的新手,由于某种原因,在VSCode上,尝试访问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>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

.props错误的屏幕截图

但是,该项目不会引发编译时错误,并且网站运行正常。

应用程序的屏幕截图正常运行,没有Chrome控制台或终端错误

我想这少了我的代码,更因此与打字稿或某种预设配置使用Javascript有麻烦识别.props属性为每个组件,因为我有类似的问题,当我建立VScode做阵营教程项目进我的编辑器(我什至可以肯定地从站点复制粘贴了最终的index.js代码),尽管该应用程序运行良好,没有编译时错误。

遵循React教程后相同.prop错误的屏幕截图

解决此问题的唯一方法是,如果我实际上对每个类进行硬编码并创建props属性,并将其设置为任何类似的形式,则:

语法错误唯一解决方案的屏幕截图

任何想法表示赞赏!非常感谢!即使该应用程序可以正常工作,但由于编辑器中弹出的这个毫无意义的错误,我还是有一些OCD。

PS:这是我更新的依赖项

"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"
 }
Run Code Online (Sandbox Code Playgroud)

klu*_*gjo 13

您需要使用接口和TypeScript的React.Component的通用实现来定义道具和状态的外观

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>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 我将.tsx使用TypeScript-> 更改文件扩展名以指示它是一个React文件Recipe.tsx
  • 请调整类型(字符串)以适合您的数据。
  • 使用IRecipeState来定义你的组件状态的结构(this.state.fooBar)。现在可以将其留空,因为您不使用状态。
  • 确保对抛出错误(RecipeList.js)的其他组件执行相同的操作


ska*_*_23 10

您还可以通过以下方式解决此问题

class Recipe extends React.Component<any, any>{

....
....

// The rest of your normal code
}

Run Code Online (Sandbox Code Playgroud)


小智 5

基于克卢格霍斯的回答。您可以对 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>
    )
}
Run Code Online (Sandbox Code Playgroud)