简体   繁体   English

在 React 中使用对象数组。 迭代错误说我不能使用对象,请改用数组

[英]Working with a array of objects in React. Iterating error saying I cannot use object, use array instead

I'm working on a React portfolio project.我正在做一个 React 投资组合项目。 I have an external data.js file with an array of objects, which I import into the JS file I'm working in. I want to iterate over the array and place each object into it's own div.我有一个包含对象数组的外部 data.js 文件,我将其导入到我正在使用的 JS 文件中。我想遍历该数组并将每个对象放入它自己的 div 中。

I already tried an If Loop and now working with Map().我已经尝试过 If 循环,现在使用 Map()。 This is my code:这是我的代码:

const Project = () => {

    const ProjectItem = () => (
      ProjectenList.map(project => (
        <div key={project.id}>
          <div>{project}</div>
        </div>
      ))
    )


    return (
      <div className='project-kader'>
        <h1 className='title'><ProjectItem /></h1>
      </div>
    )
  }

I don't get the problem of iterating trough an array of objects.我没有遇到遍历对象数组的问题。 This is the error:这是错误:

Error: Objects are not valid as a React child (found: object with keys {-list of keys-}).错误:对象作为 React 子对象无效(找到:带有键 {-list of keys-} 的对象)。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

I must overlook a simple thing, but I'm a little stuck at the moment :-)我必须忽略一件简单的事情,但我现在有点卡住了:-)

From the fact you're using project.id (and the error message from React), we can assume that project is an object, which means you're trying to use an object as a React child here:从你使用project.id的事实(以及来自 React 的错误消息),我们可以假设project是一个对象,这意味着你试图在这里使用一个对象作为 React 子项:

<div>{project}</div>

The error is that you can't do that.错误是你不能这样做。 Instead, use properties from the object to come up with how you want it displayed.相反,使用对象的属性来想出您希望它如何显示。 For instance, if project has a description property:例如,如果project有一个description属性:

<div>{project.description}</div>

Or if it has (say) a description and a number of days estimated for the project, you might do:或者,如果它有(比如说)项目的描述和估计的天数,你可以这样做:

<div>{project.description} - {project.days} {project.days === 1 ? "day" : "days"}</div>

And so on.等等。 The fundamental thing is to provide React with something it can put in the DOM (loosely, "display"), such as strings, numbers, arrays, true (but not false )...最基本的事情是为 React 提供一些它可以放入 DOM 的东西(松散地,“显示”),例如字符串,数字,数组, true (但不是false )......

You need to return array from your ProjectItem ie you need to do this:您需要从ProjectItem返回数组,即您需要这样做:

const Project = () => {

    const ProjectItem = () => (
      ProjectenList.map(project => (
        <div key={project.id}>
          <div>{project}</div>
        </div>
      ))
    )


    return (
      <div className='project-kader'>
        <h1 className='title'>{ProjectItem()}</h1>
      </div>
    )
  }

try JSON.stringy(project) like this or you should use your object property.Let's we say project has a property that called name像这样尝试 JSON.stringy(project) 或者你应该使用你的对象属性。假设项目有一个名为 name 的属性

Here is a working example for you.这是一个适合您的工作示例。

const Project = () => {

const ProjectItem = () => (
  ProjectenList.map(project => (
    <div key={project.id}>
      <div>{JSON.stringy(project)} || {project.name}</div>
    </div>
  ))
)


return (
  <div className='project-kader'>
    <h1 className='title'><ProjectItem /></h1>
  </div>
)

I hope it can work.我希望它可以工作。

you can use project as object into div element您可以将项目作为对象使用到 div 元素中

const ProjectItem = () => (
        ProjectenList.map(project => (
            <div key={project.id}>
                <div>{project.text}</div>
            </div>
        ))
)

暂无
暂无

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

相关问题 错误:对象作为 React 子对象无效(找到:[object Promise])。 如果您打算渲染一组子项,请改用数组 - Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead 对象作为 React 子对象无效 - 请改用数组 - Objects are not valid as a React child - use array instead 我不断收到此错误 Objects are not valid as a React child {} 如果您要呈现子集合,请改用数组 - I keep getting this error Objects are not valid as a React child {} If you meant to render a collection of children, use an array instead 反应。 如何将setState中的键与数组索引一起使用? - React. How to use the keys in setState with array indexes? 无法显示 ReactJS 中的数据。错误:对象作为 React 子项无效。 如果您打算呈现子集合,请改用数组 - Cannot display data in ReactJS. Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 错误:对象作为 React 子对象无效(找到:object 和键 {value})。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead 我的错误:对象作为 React 子对象无效(找到:带有键 {} 的对象)。 如果您打算渲染一组孩子,请改用数组 - My error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子项无效(找到:[object Promise])。 如果您打算渲染一组孩子,请改用数组? 解决? - Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead? Solve? 错误:对象作为 React 子对象无效(找到:[object Promise])。 如果您打算渲染一组子项,请改用数组。 (下一个) - Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead. (Next) 错误:对象作为 React 子级无效(找到:object 和键 {rank})。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child (found: object with keys {rank}). If you meant to render a collection of children, use an array instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM