最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Each child in an array or iterator should have a unique "key" prop. in react js? - Stack Overflow

programmeradmin3浏览0评论

I already provide different key .Why it is giving me warning ? here is my code

<TableBody>
              {fetchData.map(([title, row], index) => {
                return (
                  <Fragment>
                    <TableRow key={index}>
                      <TableCell colSpan="2">{title}</TableCell>
                    </TableRow>
                    {row.map(({ displaytext, value }, i) => (
                      <TableRow key={index + i}>
                        <TableCell>{displaytext}</TableCell>
                        <TableCell>{value}</TableCell>
                      </TableRow>
                    ))}
                  </Fragment>
                );
              })}
            </TableBody>

I already provide different key .Why it is giving me warning ? here is my code https://codesandbox.io/s/0xvqw6159n

<TableBody>
              {fetchData.map(([title, row], index) => {
                return (
                  <Fragment>
                    <TableRow key={index}>
                      <TableCell colSpan="2">{title}</TableCell>
                    </TableRow>
                    {row.map(({ displaytext, value }, i) => (
                      <TableRow key={index + i}>
                        <TableCell>{displaytext}</TableCell>
                        <TableCell>{value}</TableCell>
                      </TableRow>
                    ))}
                  </Fragment>
                );
              })}
            </TableBody>
Share Improve this question edited Dec 10, 2018 at 16:37 kind user 41.9k8 gold badges68 silver badges78 bronze badges asked Dec 8, 2018 at 11:27 user5711656user5711656 3,6786 gold badges42 silver badges81 bronze badges 4
  • index + i does not guarantee uniqueness. – kind user Commented Dec 8, 2018 at 11:31
  • 1 You have duplicates. When index is 0 you create 0 + 0, 0 + 1, 0 + N and then index goes to 1 and you have 1 + 0 and so on... – Adriano Repetti Commented Dec 8, 2018 at 11:31
  • please suggest better way – user5711656 Commented Dec 8, 2018 at 11:39
  • codesandbox.io/s/0xvqw6159n I added one but still same error – user5711656 Commented Dec 8, 2018 at 11:40
Add a comment  | 

3 Answers 3

Reset to default 21

First of all, you are setting the key at wrong line. Fragment does not render into HTML, however it requires the key prop.

<Fragment key={index}>
   <TableRow>

Secondly, I would suggest you to add an unique id to your nested properties, since index + i does not guarantee uniqueness.

index + i isn't unique for every value of index and i.

Let's say fetchData and row are both of length 2, so index and i will both count from 0 to 1.

key = index + i

index  i     key
0      0     0
0      1     1
1      0     1
1      1     2

This produces 1 twice, so the keys aren't unique. Converting to string would help:

key = String( index ) + String( i )

index  i     key
0      0     "00"
0      1     "01"
1      0     "10"
1      1     "11"

Or you can multiply by row.length to produce unique integers.

key = ( index * row.length ) + i

index  i     key
0      0     0
0      1     1
1      0     2
1      1     3

Are your title or display text unique? If so, you can use them as key props.

<TableBody>
          {fetchData.map(([title, row], index) => {
            return (
              <Fragment>
                <TableRow key={title}>
                  <TableCell colSpan="2">{title}</TableCell>
                </TableRow>
                {row.map(({ displaytext, value }, i) => (
                  <TableRow key={displaytext}>
                    <TableCell>{displaytext}</TableCell>
                    <TableCell>{value}</TableCell>
                  </TableRow>
                ))}
              </Fragment>
            );
          })}
</TableBody>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论