javascript - 正在渲染对象数组的数组,不断获取 "Warning: Each child in a list should have a unique "键“prop”。

标签 javascript arrays reactjs key

我有一个包含数组的对象。我能够让它按照我想要的方式呈现。然而,React 键给我带来了麻烦;抛出此“警告:列表中的每个子项都应该有一个唯一的“key” Prop 。”

我尝试在原始数组对象中为每个数组中的每个元素提供唯一的“id”属性,但没有帮助。

我也浏览了这些,但我想我有所有的建议: 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'));

这是回调顺序:

应用程序 -> 类(class) -> 类(class) -> {标题、内容 -> 部分}

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>
        </>
    )
}


类(class)和内容组件遇到问题。

警告屏幕截图:https://drive.google.com/open?id=1kcLlF0d90BG6LXPojDtfSlBFGafC9HC_

最佳答案

我认为问题出在这里:

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

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

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很好地解释了为什么您需要这样做。

关于javascript - 正在渲染对象数组的数组,不断获取 "Warning: Each child in a list should have a unique "键“prop”。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57838591/

相关文章:

javascript - 添加新页面到reactjs模板

javascript - 处理 Angular js 中的下拉菜单

python - Numpy 来自数组,为每个元素创建一个矩阵 N*M,其中所有值都设置为元素,无需 for 循环

javascript - ReactJS : only render selected child vs. 中的选项卡容器使用 `display:none`

javascript - 在浅层渲染的功能组件上调用 instance() 返回 null

javascript - 在 JavaScript 中使用 # 字符导出 CSV 数据不起作用

javascript - Angular 5 添加组件的新实例

php - 来自bind_result的数字打破了php数组

c++ - 使用指针创建二维动态数组

javascript - 如何修复 gatsbyjs 中的 "Unknown argument ' slug' "?