简体   繁体   English

由于react组件的key值出现错误但是不知道怎么改

[英]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.

相关问题 输入评论后出现关键错误。 我不知道如何解决(反应问题) - I got a key error after entering a comment. I don't know how to fix (react propblem) 不知道键值时,如何从JSON响应中检索键? - How do I retrieve a key from a JSON response, when I don't know the key value? 用键在对象中查找特定值,但我不知道路径 - Find a specific value in a object with key but i don't know the path 我不知道该怎么办,因为发生错误-&gt; Uncaught TypeError:无法读取null的属性&#39;getContext&#39; - I don't know what to do because an error occurs --> Uncaught TypeError: Cannot read property 'getContext' of null 我不知道如何更改模态的状态 - I don't know how to change the status of a modal 我不知道如何更改此滑块的箭头(左右) - I Don't know how to Change the arrows( left and right) for this slider 如果我不知道哪些键已经存在,如何将键值对添加到JSON列表的末尾? - How can I add a key value pair to the end of a JSON list if I don't know which keys already exist? 不知道为什么在将对象数组作为道具传递给反应 js 中的子组件时出现错误 - don't know why im getting error while passing array of objects as props to child component in react js 由于 javascript 获取 PHP 值,代码无法正常运行,我不知道如何比较它们 - Code not running properly due to javascript getting PHP values and i don't know how to compare them 当我设置初始值时,React Input 不会改变 - React Input don't change when I set a initial value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM