简体   繁体   English

我在使用 React 制作的项目中遇到未定义的错误

[英]I'm getting an undefined error in my project made with React

I am sending as a prop an array of objects.我正在发送一组对象作为道具。 When I console.log(this.props) I get the array of objects, but when I try to assign it to a variable it gives me当我console.log(this.props)我得到了对象数组,但是当我尝试将它分配给一个变量时,它给了我

TypeError:ninjas is undefined类型错误:忍者未定义

This is how i send the prop这就是我发送道具的方式

import React from 'react';
import Ninjas from './Ninjas';

class App extends React.Component {
  state = {
    ninjas:[
      {name:"Ryu",age:"20",belt:"black",key:"1"},
      {name:"Yoshi",age:"22",belt:"yellow",key:"2"},
      {name:"Mario",age:"25",belt:"white",key:"1"}
    ]
  }
  render(){
  return (
    <div>
      <h1>My first React App</h1>
      <Ninjas list={ this.state.ninjas }/>
    </div>
  )
  }
}

export default App;

And this is how i recibe it这就是我的食谱

import React from 'react';

class Ninjas extends React.Component {
    render(){
        const { ninjas } = this.props;
        const ninjasList = ninjas.map(ninja => {
            return(
                <div className="ninja" key={ ninja.key }>
                <div>Name: { ninja.name }</div>
                <div>Age: { ninja.age }</div>
                <div>Belt: { ninja.belt }</div>
                </div>
            )
        })
        return (
            <div className="ninja-list">
                { ninjasList }
            </div>
        );
    }
}

export default Ninjas;
<Ninjas list={ this.state.ninjas }/>

I suggest you change this to我建议你把这个改成

<Ninjas ninjas={ this.state.ninjas }/>

Otherwise the name would be list in your child component.否则该名称将list在您的子组件中。

In other words the name of the property you use when rendering the component (here in the render function of App ) has to correspond to the name you get from the props object in your child component (here your child component is Ninjas ).换句话说,您在渲染组件时使用的属性名称(此处为Apprender函数)必须与您从子组件中的props对象获得的名称相对应(此处您的子组件为Ninjas )。

You are passing ninjas in your Ninjas component <Ninjas list={ this.state.ninjas }/> using list props.您正在使用列表道具在 Ninjas 组件<Ninjas list={ this.state.ninjas }/>中传递ninjas So, you should be using this const { list } = this.props;所以,你应该使用这个const { list } = this.props; instead of const { ninjas } = this.props;而不是const { ninjas } = this.props; in your Ninjas Component.在你的 Ninjas 组件中。

import React from 'react';

class Ninjas extends React.Component {
    render(){
        const { list } = this.props;
        const ninjasList = list.map(ninja => {
            return(
                <div className="ninja" key={ ninja.key }>
                <div>Name: { ninja.name }</div>
                <div>Age: { ninja.age }</div>
                <div>Belt: { ninja.belt }</div>
                </div>
            )
        })
        return (
            <div className="ninja-list">
                { ninjasList }
            </div>
        );
    }
}

export default Ninjas;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在我的 React 项目中收到此错误“TypeError: Cannot read property 'map' of undefined” - I'm getting this error “TypeError: Cannot read property 'map' of undefined” on my React project 我在我的反应项目中收到此错误“TypeError: toLocation is undefined” - I get this error 'TypeError: toLocation is undefined' in my react project 创建我的 React 项目后。当我在 src 中创建组件和上下文文件夹时,我收到此错误。我该怎么办? - After Creating my React Project.When I made Components and Context folder in src I am getting this error.What should I do? 在 JS 中迭代我的数组,但出现“无法读取未定义的属性‘长度’”错误 - Iterating on my array in JS but I'm getting a "Cannot read property 'length' of undefined" error 尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性 - I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props 获取 TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function 在我的 React 组件中,我不知道为什么 - Getting TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function in my React component and I'm not sure why 我在 React 中收到“TypeError: Cannot read properties of undefined(reading: 0)” - I'm getting "TypeError: Cannot read properties of undefined(reading: 0)" in React 当我试图启动我的反应应用程序时,我收到一个不寻常的错误。 它说CleanWebpackPlugin不是构造函数 - I'm getting an unusual error when I'm trying to start my react app. It says that CleanWebpackPlugin is not a constructor 尝试遍历我的 json 时,我变得未定义 - I'm getting undefined when trying to iterate through my json 我收到错误:运行我的反应项目时无法读取未定义的属性'then' - I get the error : Cannot read property 'then' of undefined when I run my react project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM