数组的返回值在 React 中是否是“undefined”?

huangapple go评论72阅读模式
英文:

Is return value of a array "undefined" in React?

问题

我知道这很基础,但我遇到了以下错误或 bug。需要帮助

我试图使用简单的数组方法,在 React 中使用基本方法将值返回到控制台和屏幕上,但控制台上出现了 **undefined** 错误。

以下是 APP.js 的 React 代码。

import React from 'react';

function App(){

const [employees, setEmployees] = React.useState([{
id: 1,
fullName: "Bob Jones",
designation: "JavaScript Developer",
gender: "male",
teamName: "TeamA"
},
{
id: 2,
fullName: "Jill Bailey",
designation: "Node Developer",
gender: "female",
teamName: "TeamA"
},
{
id: 3,
fullName: "Gail Shepherd",
designation: "Java Developer",
gender: "female",
teamName: "TeamA"
}]);

console.log(employees.fullName)
return(

{employees.fullName}


)
}

export default App


**输出:**
控制台: Undefined
屏幕上: 空白

需要帮助解决这个错误。

我尝试了不同的已知方法,比如移动到组件中,但都没有用。我期望能在屏幕和控制台上显示数据。
英文:

I know its basic but i am stuck with the following bug or error. Need help

I am trying to use simple array method to return value on console and on screen using basic methods in react but it is giving me undefined error on console.

Following are the code of React of APP.js.

import React from 'react'

function App(){

  const [employees, setemployees] = React.useState([{
    id: 1,
    fullName: "Bob Jones",
    designation: "JavaScript Developer",
    gender: "male",
    teamName: "TeamA"
  },
  {
    id: 2,
    fullName: "Jill Bailey",
    designation: "Node Developer",
    gender: "female",
    teamName: "TeamA"
  },
  {
    id: 3,
    fullName: "Gail Shepherd",
    designation: "Java Developer",
    gender: "female",
    teamName: "TeamA"
  }]);

console.log(employees.fullName)
  return( 
    <main>
        <h1>{employees.fullName}<h1/>
    </main>
  )
}

export default App

Output:
Console: Undefined
On Screen: empty

Need help how to solve this error.

I tried different known methods like moving into components but does not work. I am expecting to show data on screen and on console.

答案1

得分: 2

错误了,因为你无法访问 employees.fullName。至少应该是 employees[0].fullName 来访问 employees 数组中的第一个对象。

console.log(employees[0].fullName);

如果你想将所有的名字作为 h1 渲染,可以这样做。

{employees.map(employee => <h1 key={employee.id}>{employee.fullName}</h1>)}
英文:

it must be error. because you can't access employees.fullName. it must be at least employees[0].fullName to access first object inside employees array.

console.log(employees[0].fullName);

if you want to render all names as an h1 you can do this.

{employees.map(employee =&gt; &lt;h1 key={employee.id}&gt;{employee.fullName}&lt;/h1&gt;)}

答案2

得分: 1

你遇到的错误信息可能是"Cannot read property 'fullName' of undefined." 这是因为employees是一个对象数组,而不是单个对象,所以直接在employees上访问fullName这样的属性是行不通的。

要修复这个问题,你需要遍历employees数组并渲染每个个体员工。一种方法是使用map函数来迭代数组并为每个员工创建一个新的元素数组。

英文:

The error message you're encountering is likely "Cannot read property 'fullName' of undefined." This is because employees is an array of objects and not a single object, so trying to access a property like fullName directly on employees will not work.

To fix this, you need to loop over the employees array and render each individual employee. One way to do this is by using the map function to iterate over the array and create a new array of elements for each employee.

答案3

得分: 0

employees[0].fullName应该可以实现

你能否提供更多细节?顺便问一下,你有尝试编写类似以下的内容吗:

<div>
  <ol> 
    {employees.map(employee => { <li key={employee.fullName}>{employee.fullName}</li>} 
  </ol> 
</div>
英文:
employees[0].fullName

should do it

Can you please give more details? btw, have you tried to write something like:

&lt;div&gt;
 &lt;ol&gt; 
{employees.map(employee =&gt; { &lt;li key={employee.fullName}&gt;{employee.fullName}&lt;/li&gt;)} 
&lt;/ol&gt; 
&lt;/div&gt; 

huangapple
  • 本文由 发表于 2023年2月10日 06:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定