[英]An error occurs due to the key value of the react component But I don't know how to change
I changed the key value to c.createdAt c.id but got an error How can I fix it?我将键值更改为 c.createdAt c.id 但出现错误如何解决?
{me && <PostForm />}
{mainPosts.map((c) => {
console.log("c : ", c);
return (
<PostCard key={c} post={c} />
);
})}
c : {id: 35, content: "asdfasdfadf", createdAt: "2019-11-17T06:49:45.000Z", updatedAt: "2019-11-17T06:49:45.000Z", UserId: 1, …}
index.js:27 c : {id: 34, content: "asdfasdfadfadf", createdAt: "2019-11-17T06:02:45.000Z", updatedAt: "2019-11-17T06:02:45.000Z", UserId: 1, …}
index.js:27 c : {id: 33, content: "asdfasdf 1111", createdAt: "2019-11-16T11:01:24.000Z", updatedAt: "2019-11-16T11:01:24.000Z", UserId: 1, …}
index.js:27 c : {id: 32, content: "asdfaa 1111", createdAt: "2019-11-16T10:45:37.000Z", updatedAt: "2019-11-16T10:45:37.000Z", UserId: 1, …}
error message is this:错误信息是这样的:
Uncaught TypeError: Cannot read property 'key' of undefined
at List._this.renderItem (:3060/_next/static/development/pages/_app.js?ts=1574030957536:22355)
at :3060/_next/static/development/pages/_app.js?ts=1574030957536:22465
at Array.map (<anonymous>)
at List._this.renderList (:3060/_next/static/development/pages/_app.js?ts=1574030957536:22464)
at updateContextConsumer (:3060/_next/static/development
I don't know where the key error occurs不知道关键错误发生在哪里
I'm changing all the key values but it doesn't work我正在更改所有键值,但它不起作用
The Problem with your code is you are passing an object in your <PostCard/>
component' key.您的代码的问题是您在<PostCard/>
组件的键中传递了 object。 this needs to be either string or integer.这需要是字符串或 integer。
rewrite your return code to将您的返回码重写为
<PostCard key={c.id} post={c} />
because c
is an object因为c
是 object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.