简体   繁体   English

反应错误类型错误:无法读取未定义的属性“名称”

[英]React Error TypeError: Cannot read property 'name' of undefined

I am learning to react but I am stuck in one issue.我正在学习做出反应,但我陷入了一个问题。 I am trying to get the array properties in my Useritem.js file我正在尝试在我的 Useritem.js 文件中获取数组属性

this is my User.js file这是我的 User.js 文件

import React, { Component } from 'react'
import Useritems from './Components/Useritems';


const User = () => {
    state = {
        users: [
            { name: 'David', age: '20', Position: 'developer' },
            { name: 'Francis', age: '20', Position: 'Software Engineer' },
        ]
    }
    return (
        <div>
            {this.state.users.map(user => (
                <Useritems user={user} />
            ))}
        </div>
    );
}
export default User

And this is my Useritem.js file这是我的 Useritem.js 文件


import React from 'react'

const Useritems = ({ user: { name, age, job } }) => {
    return (
        <div>
            <h1>{name}</h1>
            <h2>{age}</h2>
            <h3>{job}</h3>
        </div>
    )

}

export default Useritems

But I am getting an error saying that the property name is not defined但我收到一条错误消息,指出未定义属性名称

You are using a functional component (as opposed to a class component), so you cannot use this.state .您正在使用功能组件(而不是 class 组件),因此您不能使用this.state Instead you need to use the hook useState to handle your state, which you can do like this:相反,您需要使用钩子useState来处理您的 state,您可以这样做:

import React, { useState } from "react";
import Useritems from "./Components/Useritems";

const initialUsers = [
  { name: "David", age: "20", Position: "developer" },
  { name: "Francis", age: "20", Position: "Software Engineer" }
];

const Users = () => {
  const [users, setUsers] = useState(initialUsers);

  return (
    <div>
      {users.map((user) => (
        <Useritems key={user.name} user={user} /> // don't forget the key, though will want to find something more unique than name!
      ))}
    </div>
  );
};

暂无
暂无

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

相关问题 类型错误:无法读取未定义反应错误的属性“名称” - TypeError: Cannot read property 'name' of undefined react error 反应:类型错误:无法读取未定义的属性“名称” - REACT: TypeError: Cannot read property 'name' of undefined “ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - “TypeError: Cannot read property 'name' of undefined” error Uncaught TypeError:无法读取React中未定义的属性“名称” - Uncaught TypeError: Cannot read property 'name' of undefined in React React - 未捕获的类型错误:无法使用 Airtable 读取未定义的属性“名称” - React - Uncaught TypeError: Cannot read property "name' of undefined using Airtable 反应:未捕获的类型错误:无法读取 mapStateToProps 中未定义的属性“名称” - React: Uncaught TypeError: Cannot read property 'name' of undefined in mapStateToProps TypeError:无法读取 React 中未定义的属性“多对象中的状态名称” - TypeError: Cannot read property 'state name in much object' of undefined in React React Native TypeError:无法读取未定义的属性“名称” - React Native TypeError: Cannot read property 'name' of undefined 当我想访问对象详细信息时,React JS引发“ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - React JS throw “TypeError: Cannot read property 'name' of undefined” error when i want to access object details 渲染错误:“ TypeError:无法读取未定义的属性&#39;名称&#39;” [Vue警告] - Error in render: “TypeError: Cannot read property 'name' of undefined” [Vue warn]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM