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

44

我在尝试访问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>
        )
    }
}

.props错误截图

然而,该项目并没有抛出编译时错误,并且网站运行得非常好。

应用程序正常工作,没有Chrome控制台或终端错误的截图

我认为这与我的代码关系不大,更多地与TypeScript或某种预设配置有关,因为VScode在识别每个组件的.props属性时遇到了麻烦,当我将React教程项目构建到我的编辑器中时(我甚至复制粘贴了网站上的最终index.js代码以确保正确),我遇到了类似的问题,尽管应用程序没有编译时错误,也能正常工作。

按照React教程后相同的.prop错误截图

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

仅有的语法错误解决方案截图

以下是我的更新依赖项:

"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"
 }

1
“Property 'props' does not exist on type 'Home'"(https://dev59.com/Z1YN5IYBdhLWcg3wdoAY?rq=1)或者“TypeScript Property 'props' does not exist”(https://dev59.com/jVcP5IYBdhLWcg3wL3YN#48260148)有帮助吗?他们推荐使用`@types/react`库。 - SamVK
可能是重复的问题,参考 TS2339:在类型“Home”上不存在属性“props” - Matt McCutchen
1
很不幸,当我添加@types/react库时,我遇到了类似的错误:“在类型'Readonly<{ children?: ReactNode; }> & Readonly<{}>'上不存在属性'recipes'。” - LoopBoi
这个回答解决了你的问题吗?属性'value'在类型'Readonly<{}>'上不存在 - Michael Freidgeim
3个回答

80

您需要使用接口和TypeScript的泛型实现来定义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),也要执行相同的操作。

10

您也可以使用以下方法解决此问题:

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

....
....

// The rest of your normal code
}


5

基于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>
    )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接