正在渲染对象阵列的阵列时,不断得到“警告”:列表中的每个子代都应具有唯一的“键”。道具”。 [英] Was rendering arrays of arrays of objects, keep getting "Warning: Each child in a list should have a unique "key" prop."

查看:96
本文介绍了正在渲染对象阵列的阵列时,不断得到“警告”:列表中的每个子代都应具有唯一的“键”。道具”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数组数组的对象。我能够按照我想要的方式进行渲染。但是,React密钥给我带来了麻烦。抛出此警告:列表中的每个孩子都应该有一个唯一的键道具。

I have an object that contains array of arrays. I was able to make it render the way I wanted. However, the React key is giving me trouble; throwing this "Warning: Each child in a list should have a unique "key" prop."

我尝试在原始数组对象中赋予唯一的'id'属性对于每个数组中的每个元素,但没有帮助。

I have tried giving a unique 'id' property within the original array object for each element in each array but did not help.

我也仔细研究了这些,但是我想那里有所有建议:
每个孩子数组或迭代器中的字符串应具有唯一的键 reactjs中的道具
警告:列表中的每个孩子应该有一个唯一的键。道具
警告:数组或迭代器中的每个子代都应具有唯一的 key键。支柱。检查`ListView`的渲染方法

I also looked through these but I think I have all of the suggestions there: Each child in an array or iterator should have a unique "key" prop in reactjs Warning: Each child in a list should have a unique "key" prop Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`

我的对象

const courses = 
[
    {
      name: 'Half Stack application development',
      id: 'Half Stack application development',
      parts: [
        {
          name: 'Fundamentals of React',
          exercises: 10,
          id: 'Fundamentals of React'
        },
        {
          name: 'Using props to pass data',
          exercises: 7,
          id: 'Using props to pass data'
        },
        {
          name: 'State of a component',
          exercises: 14,
          id: 'State of a component'
        },
        {
          name: 'Redux',
          exercises: 11,
          id: 'Redux'
        }
      ]
    }, 
    {
      name: 'Node.js',
      id: 'Node.js',
      parts: [
        {
          name: 'Routing',
          exercises: 3,
          id: 'Routing'
        },
        {
          name: 'Middlewares',
          exercises: 7,
          id: 'Middlewares'
        }
        ]
    }
]


ReactDOM.render(<App course={courses}/>, document.getElementById('root'));

这是回调顺序:

应用->课程->课程-> {标题,内容->部分}

App -> Courses -> Course -> {Header, Content -> Part}

const App = ({course}) => {
  return (
    <>
      <Courses course={course} />
    </>
  )
}

const Courses = ({course}) => {
    console.log("Starting.....")
    const courseList = course.map(nameConent => nameConent)
    // console.log(courseList)
    // const idList = courseList.map(eachCourse =>eachCourse.id)
    const mapEverything = () => courseList.map(a => <Course name={a.name} partsList={a.parts} id={a.id}/>)
    // const mapEverything = () => courseList.map(a => console.log(a.id))
    // console.log("CourseID",idList)
    return (
        <>  
            {mapEverything()}
        </>

    )
}

const Course = ({name,partsList,id}) => {
    // console.log(name)
    console.log("CourseID", id)

    return (
        <>
            <div key={id}>
                <Header header={name} id={id}/>
                <Content contents={partsList} id={id+"======"}/>
            </div>
        </>
    )
}

const Content = ({contents,id}) => {
    console.log("contentsID",id)
    const partList = () => contents.map(part =>
        <Part
            name={part.name}
            id={part.id} 
            exercises={part.exercises}
        />
    )

    const getSumofArray = (accumulator, currentValue) => accumulator + currentValue

    const exeList = contents.map(content => content.exercises).reduce(getSumofArray)
    // console.log(exeList)
    return (
        <>
            <ul key={id}>
                {partList()}
            </ul>
            <b>total exercises: {exeList}</b>
        </>
    )
}

const Header = ({header, id}) => {
    console.log("HeaderID", id)
    return (
        <>
            <h1 key={id}>
                {header}
            </h1>
        </>
    )
}

const Part = ({name, id, exercises}) => {
    console.log("PartID", id)
    return (
        <>
            <li  key={id}>
                {name}: {exercises}
            </li>
        </>
    )
}


在课程和内容部分遇到了麻烦。

It's having trouble at the Courses and Content component.

警告屏幕截图: https://drive.google.com/open?id=1kcLlF0d90BG6LXPojDtfslBFGafC9HC_> b $ b

screenshot of warnings: https://drive.google.com/open?id=1kcLlF0d90BG6LXPojDtfSlBFGafC9HC_

推荐答案

我认为这里是个问题:

const mapEverything = () => courseList.map(a => <Course name={a.name} partsList={a.parts} id={a.id}/>)

您应该尝试在此处添加键:

You should try add key here:

const mapEverything = () => courseList.map(a => <Course name={a.name} partsList={a.parts} id={a.id} key={a.id}/>)

这里也是:

const partList = () => contents.map(part =>
        <Part
            name={part.name}
            id={part.id} 
            exercises={part.exercises}
            key={part.id}
        />)

此处很好地解释了为什么需要这样做

Here is a good explanation of why you need to do it this way.

这篇关于正在渲染对象阵列的阵列时,不断得到“警告”:列表中的每个子代都应具有唯一的“键”。道具”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆