Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!