Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
React Warning eliminate Warning: Each child in a list should have a unique "key" prop

React Warning eliminate Warning: Each child in a list should have a unique "key" prop.

I would like to eliminate the 「Warning: Each child in a list should have a unique "key" prop.」

The code is as follows

import { useState } from "react";

const SIZE_ARRAY = [
  {
    label: "Small",
    value: "sm"
  },
  {
    label: "Medium",
    value: "md"
  },
  {
    label: "Large",
    value: "lg"
  }
];

const DEVICE_ARRAY = [
  {
    deviceLabel: "PC",
    deviceValue: "pc"
  },
  {
    deviceLabel: "Tablet",
    deviceValue: "tablet"
  },
  {
    deviceLabel: "Mobile",
    deviceValue: "mobile"
  }
];

export default function SampleLoop() {
  const [option, setOption] = useState();

  return (
    <>
      <ul>
        {SIZE_ARRAY.map((size) => {
          const { label, value } = size;
          return (
            <li key={label}>
              <span>Margin : {label}</span>
              {DEVICE_ARRAY.map((device) => {
                const { deviceLabel, deviceValue } = device;
                return (
                  <>
                    <input
                      key={deviceLabel}
                      onChange={(newValue) => {
                        setOption({
                          ...option,
                          margin_size: {
                            ...option.margin_size,
                            [value]: {
                              ...option.margin_size[value],
                              [deviceValue]: newValue
                            }
                          }
                        });
                      }}
                    />
                  </>
                );
              })}
            </li>
          );
        })}
      </ul>
    </>
  );
}

codesandbox

It is a nested form of two loops.

Is there a different position to attach the KEY?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In here:

{DEVICE_ARRAY.map((device) => {
  const { deviceLabel, deviceValue } = device;
  return (
    <>
      <input
        key={deviceLabel}
        onChange={(newValue) => {
          setOption({
            ...option,
            margin_size: {
              ...option.margin_size,
              [value]: {
                ...option.margin_size[value],
                [deviceValue]: newValue
              }
            }
          });
        }}
      />
    </>
  );
})}

The "root element" of each child is the </>, i.e. <Fragment/>, not <input/>. So you should define the key in the former instead. Although quite frankly, you don't need that Fragment here unless you omitted some components in your posted code.

BTW, you can't use the fragment shorthand if you specify any props in it. i.e. this doesn't work:

< key={deviceLabel}>
</>

Instead, do this

<Fragment key={deviceLabel}>
</Fragment>
about 4 years ago · Juan Pablo Isaza Relatório

0

Remove the redundant fragment around <input>

import { useState } from "react";

const SIZE_ARRAY = [
  {
    label: "Small",
    value: "sm"
  },
  {
    label: "Medium",
    value: "md"
  },
  {
    label: "Large",
    value: "lg"
  }
];

const DEVICE_ARRAY = [
  {
    deviceLabel: "PC",
    deviceValue: "pc"
  },
  {
    deviceLabel: "Tablet",
    deviceValue: "tablet"
  },
  {
    deviceLabel: "Mobile",
    deviceValue: "mobile"
  }
];

export default function SampleLoop() {
  const [option, setOption] = useState();

  return (
    <ul>
      {SIZE_ARRAY.map((size) => {
        const { label, value } = size;
        return (
          <li key={label}>
            <span>Margin : {label}</span>
            {DEVICE_ARRAY.map((device) => {
              const { deviceLabel, deviceValue } = device;
              return (
                <input
                  key={deviceLabel}
                  onChange={(newValue) => {
                    setOption({
                      ...option,
                      margin_size: {
                        ...option.margin_size,
                        [value]: {
                          ...option.margin_size[value],
                          [deviceValue]: newValue
                        }
                      }
                    });
                  }}
                />
              );
            })}
          </li>
        );
      })}
    </ul>
  );
}

Edit dank-flower-u0pch0

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda