Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda