gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:53:08 25 4
gpt4 key购买 nike

我有一个包含数组数组的对象。我能够按照我想要的方式渲染它。但是,React 键给我带来了麻烦;抛出这个“警告:列表中的每个 child 都应该有一个独特的“关键” 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'));

这是回调顺序:

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


类(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com