我有一个包含数组数组的对象。我能够按照我想要的方式进行渲染。但是,React密钥给我带来了麻烦。抛出此“警告:列表中的每个孩子都应该有一个唯一的“关键”道具。”
我尝试在原始数组对象中为每个数组中的每个元素赋予唯一的'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'));
这是回调顺序:
应用->课程->课程-> {标题,内容->部分}
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>
</>
)
}
“课程和内容”组件出现问题。
警告屏幕截图:https://drive.google.com/open?id=1kcLlF0d90BG6LXPojDtfSlBFGafC9HC_
答案 0 :(得分:2)
我认为这里是个问题:
const mapEverything = () => courseList.map(a => <Course name={a.name} partsList={a.parts} id={a.id}/>)
您应该尝试在此处添加密钥:
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是为什么需要这样做的很好解释。
答案 1 :(得分:0)
这是关键道具的主题。每当我们在Reactjs中呈现数组或列表时,它都希望有一个可用的key属性。
此关键属性的存在是出于性能方面的考虑,它使Reactjs更易于呈现项目列表。
您要做的就是为数组中的任何其他元素提供另一个支持。您在课程列表中拥有的唯一标识符似乎是名字。因此,您可能希望将代码简化为类似的形式,以使自己继续前进:
const CourseList = () => {
const RenderedCourses = courses.map(function(course) {
return <CourseDetail key={course.name} course={course} />;
});
return <ul>{RenderedCourses}</ul>;
};
然后,一旦您能够正常工作,就可以使用箭头功能和所有代码简化代码的使用方式:
const CourseList = () => {
const RenderedCourses = courses.map(course => (
<CourseDetail key={course.name} course={course} />
));
return <ul>{RenderedCourses}</ul>;
};