Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 财产';XYZ';不存在于类型';只读<;{children?:ReactNode;}>&;只读<;{}>';_Javascript_Reactjs_Typescript_Visual Studio Code_Babeljs - Fatal编程技术网

Javascript 财产';XYZ';不存在于类型';只读<;{children?:ReactNode;}>&;只读<;{}>';

Javascript 财产';XYZ';不存在于类型';只读<;{children?:ReactNode;}>&;只读<;{}>';,javascript,reactjs,typescript,visual-studio-code,babeljs,Javascript,Reactjs,Typescript,Visual Studio Code,Babeljs,我在尝试访问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() { /

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

您需要使用和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>
        )
    }
}
import React,{Component}来自'React';
导入“/Recipe.css”;
接口IRecipeProps{
成分?:字符串[];
标题?:字符串;
img?:字符串;
指令?:字符串;
}
界面地产{
}
类配方扩展组件{
render(){
const components=this.props.components.map((ing,ind)=>(
  • {ing}
  • )); const{title,img,instructions}=this.props 返回( 你的渲染代码在这里 ) } }
    • 我会将文件扩展名更改为
      .tsx
      ,以表明它是一个使用TypeScript->
      Recipe.tsx
    • 请调整类型(字符串)以适合您的数据
    • 使用
      IRecipeState
      定义组件状态的结构(
      this.state.fooBar
      )。现在可以将其留空,因为您不使用状态
    • 确保对引发错误的其他组件执行相同的操作(
      RecipeList.js
      • 基于答案。您可以对React的功能组件(FC)执行相同的操作,并使用useState钩子来管理状态

        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>
            )
        }
        
        import React,{FC}来自'React';
        导入“/Recipe.css”;
        接口IRecipeProps{
        成分?:字符串[];
        标题?:字符串;
        img?:字符串;
        指令?:字符串;
        }
        界面地产{
        }
        常量配方:FC=(道具)=>{
        const{成分、标题、img、说明}=道具;
        成分图((成分,索引)=>(
        
      • {成分}
      • )); 返回( 你的渲染代码在这里 ) }
        Do或help?他们建议使用
        @types/react
        库。很遗憾,当我添加@types/react库时,会出现类似的错误“type'Readonly&Readonly'上不存在属性'recipes'。”这是否回答了您的问题?
        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>
            )
        }