简体   繁体   English

在使用 uuid 时,列表中的每个孩子都应该有一个唯一的“key”道具

[英]Each child in a list should have a unique “key” prop while using uuid

I have a recursive component which is maping over rows and columns so the indexes might be repeated if I used the index of each map function.我有一个递归组件,它映射行和列,因此如果我使用每个映射函数的索引,索引可能会重复。 Instead I used the package uuid and used v4() as a key everywhere but still getting error:相反,我使用了包uuid并在任何地方使用v4()作为键,但仍然出现错误:

  import { v4 } from "uuid";

 //....allcode before return 
        return (
          <>
            <Table.Header key={v4()} fullWidth>
              <Table.Row
                columns={size(Object.keys(data?.records[0]?.data)) + 2}
                key={v4()}
              >
                <Table.HeaderCell key={v4()}></Table.HeaderCell>
                {Object.keys(data?.records[0]?.data).map((key, ind) => {
                  return <Table.HeaderCell key={v4()}>{key}</Table.HeaderCell>;
                })}
                <Table.HeaderCell key={v4()}></Table.HeaderCell>
              </Table.Row>
            </Table.Header>
    
            {data?.records.map((firstLevel, ind1) => {
              return (
                <>
                  <Table.Body key={v4()}>
                    {/* here we render each row of data  */}
                    <Table.Row key={v4()}>
                      {size(firstLevel.kids) > 0 ? (
                        <Table.Cell key={v4()}>
                          <Icon
                            name="caret right"
                            link
                            size="large"
                            rotated={
                              show[ind1 + depth * 1000] ? "clockwise" : undefined
                            }
                            onClick={() => toggleShow(ind1, depth)}
                          />
                        </Table.Cell>
                      ) : (
                        <Table.Cell key={v4()}></Table.Cell>
                      )}
    
                      {Object.keys(firstLevel.data).map((key) => {
                        return (
                          <Table.Cell key={v4()}>{firstLevel.data[key]}</Table.Cell>
                        );
                      })}
                      <Table.Cell key={v4()}>
                        <Icon
                          name="close"
                          link
                          size="large"
                          onClick={() => removeData(ind1, depth, indArrayState)}
                        />
                      </Table.Cell>
                    </Table.Row>
                  </Table.Body>
    
                  {show[ind1 + depth * 1000] && size(firstLevel.kids) > 0 ? (
                    <>
                      {indArray.splice(depth, 1, ind1)}
    
                      <Table.Header key={v4()} fullWidth>
                        <Table.Row key={v4()}>
                          <Table.HeaderCell key={v4()}>
                            {Object.keys(firstLevel.kids)}
                          </Table.HeaderCell>
                        </Table.Row>
                      </Table.Header>
                      <RecursiveTable
                        data={firstLevel.kids[Object.keys(firstLevel.kids)[0]]}
                        allData={allData}
                        depth={depth + 1}
                        setAllData={setAllData}
                        indArray={indArray}
                      />
                    </>
                  ) : null}
                </>
              );
            })}
          </>
        );
    }

Any Idea?任何想法?

Thank you.谢谢你。

Stackoverflow:It looks like your post is mostly code; Stackoverflow:看起来你的帖子主要是代码; please add some more details.It looks like your post is mostly code;请添加更多详细信息。看起来您的帖子主要是代码; please add some more details.请添加更多详细信息。

It looks like you are missing a key on a React Fragment.看起来您在 React Fragment 上缺少一个键。 You could replace <> with <React.Fragment> and do something like the following:您可以将<>替换为<React.Fragment>并执行以下操作:

data?.records.map((firstLevel, ind1) => {
   return (
      <React.Fragment key={x}>
        ...
      </React.Fragment>>

Reference参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列表中的每个孩子都应该有一个唯一的“关键”道具错误,以 uuid 作为关键反应 - Each child in a list should have a unique "key" prop error with uuid as key react 嵌套列表迭代时,列表中的每个子项都应具有唯一的“键”道具 - Each child in a list should have a unique "key" prop while nested list iteration 列表中的每个孩子都应该有一个唯一的“key”道具。 在反应 - Each child in a list should have a unique "key" prop. in react ReactNative警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ReactNative Warning: Each child in a list should have a unique “key” prop 警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Warning Each child in a list should have a unique "key" prop 列表中的每个孩子都应该有一个唯一的“关键”道具图像 - Each child in a list should have a unique “key” prop images React,列表中的每个孩子都应该有一个唯一的 key prop - React, Each child in a list should have a unique key prop ESLint 错误:列表中的每个孩子都应该有一个唯一的“key”道具 - ESLint Error: Each child in a list should have a unique “key” prop 列表中的每个孩子都应该有一个唯一的“关键”道具控制台错误 - Each child in a list should have a unique "key" prop console error React,列表中的每个孩子都应该有一个独特的“关键”道具 - React, Each child in a list should have a unique "key" prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM