簡體   English   中英

由於react組件的key值出現錯誤但是不知道怎么改

[英]An error occurs due to the key value of the react component But I don't know how to change

我將鍵值更改為 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, …}

錯誤信息是這樣的:

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

不知道關鍵錯誤發生在哪里

我正在更改所有鍵值,但它不起作用

您的代碼的問題是您在<PostCard/>組件的鍵中傳遞了 object。 這需要是字符串或 integer。

將您的返回碼重寫為

<PostCard key={c.id} post={c} />

因為c是 object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM