简体   繁体   English

提供独特的关键道具来反应嵌套地图

[英]Give unique key props to react nested map

How to give a unique key props in react map.如何在反应地图中给出唯一的关键道具。 I use uuidv4 library but react also says every child component should have a unique key prop.我使用 uuidv4 库,但 react 还说每个子组件都应该有一个唯一的 key prop。 This is my code that I use uuidv4 npm library.这是我使用 uuidv4 npm 库的代码。

I try many ways but it not work.我尝试了很多方法,但它不起作用。

    // import Sidebar from '../components/Sidebar'
  import React from 'react'
  import Navbar from '../components/Navbar';
  import ButtonGroup from '../components/ButtonGroup';
  import MCQS from '../components/MCQ';
  import { Outlet } from 'react-router';
  import { v4 as uuidv4 } from 'uuid';
  import { useSelector } from 'react-redux';
  const C_P_HOME = () => {
    const { heading } = useSelector(state => state.heading);
    const { questions } = useSelector(state => state.question);
    return (
      <>
        <Navbar isContainerTrue={false} />
        <div className='grid grid-cols-12 '>
          <div className="col-span-6 bg-slate-100 grid grid-cols-12 p-8 h-screen items-baseline">
            <ButtonGroup />
            <Outlet />
          </div>
          <div className="col-span-6 bg-slate-200 h-screen overflow-y-scroll">
            <div className="heading select-none">
              <h1 className='text-2xl text-center font-bold mt-8'>{heading}</h1>
            </div>
            <div className="questions p-8">

              {
                questions.map((q, index) => {
                  return (
                    <>
                      <h2 className='text-2xl font-semibold' key={uuidv4()}>Q:{(index + 1) + " " + q.heading}</h2>
                      <ol className='list-[lower-roman]' key={uuidv4()}>
                        {
                          q.questions.map((ques, index) => {
                            return (
                              <>
                                <li className='text-lg ml-16' key={uuidv4()}>{ques}</li>
                              </>
                            )
                          })
                        }
                      </ol>
                    </>
                  )
                })
              }
            </div>
          </div>

        </div>
      </>
    )
  }

  export default C_P_HOME;

This is the screenshot of my error.这是我的错误截图。

这是我的错误截图

Thanks I got the answer.谢谢我得到了答案。

Usually the keys when looping over an array etc. should come from each item in the array itself.通常,遍历数组等时的键应该来自数组本身中的每个项目。 assuming your question has an id , you want to use that id as a key .假设您的question有一个id ,您想使用该id作为key since an id is usually unique, this key won't change for the item unless it is f.ex.由于 id 通常是唯一的,因此除非它是 f.ex,否则该项目的此密钥不会更改。 removed移除

Try using the below code.尝试使用下面的代码。 I created keys using index and the content of question.我使用索引和问题内容创建了键。

 {
    questions.map((q, index) => {
        return (
            <div key={`${index}_${JSON.stringify(q)}`}>
                <h2 className='text-2xl font-semibold'>
                    Q:{index + 1 + ' ' + q.heading}
                </h2>
                <ol className='list-[lower-roman]'>
                    {q.questions.map((ques, i) => (
              <li className='text-lg ml-16' key={`${ques}_${i}`}>
                {ques}
              </li>
                        ))}
                </ol>
            </div>
        );
    });
}

your problem is here你的问题在这里

 q.questions.map((ques, index) => {
                            return (
                              <>
                                <li className='text-lg ml-16' key={uuidv4()}>{ques}</li>
                              </>
                            )

key attribute needs to be on the container element inside map关键属性需要在map内的容器元素上

just remove <> </>只需删除<> </>

 q.questions.map((ques, index) => <li className='text-lg ml-16' key={uuidv4()}>{ques}</li>)

or use React.Fragment或使用React.Fragment

 q.questions.map((ques, index) => {
                            return (
                              <React.Fragment key={uuidv4()}>
                                <li className='text-lg ml-16' >{ques}</li>
                              </React.Fragment>
                            )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM