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

99
Vistas
Child list element still get re-rendered when using unique key id in React

If I understand correctly, if a unique key is provided to each list item, whenever we add new or delete existing items, the remaining ones will be re-rendered. However, I am using a unique key for the Item element and when I click the Add button, I am still seeing the render string (I added a console.log() in the Item component) been printed in console X number of times (x equals the number of total list items).

The complete code is as below:

import "./styles.css";

import { v4 as uuidv4 } from "uuid";
import { useState } from "react";

const data = () => {
  let result = [];
  for (let i = 0; i < 10; i++) {
    result.push({
      id: uuidv4(),
      value: i + 1
    });
  }
  return result;
};

function Item({ item }) {
  console.log("render");
  return (
    <li>
      <label>{item.value}</label>
    </li>
  );
}

export default function App() {
  const [state, setState] = useState(data());

  function handleAdd(e) {
    setState([
      ...state,
      {
        id: uuidv4(),
        value: state[state.length - 1].value + 1
      }
    ]);
  }

  return (
    <div className="App">
      <button onClick={handleAdd}>Add</button>
      {state.map((item) => (
        <Item item={item} key={item.id} />
      ))}
    </div>
  );
}

CodeSandbox link: https://codesandbox.io/s/react-list-with-unique-key-1rxlhx?file=/src/App.js:0-802

Did I miss anything here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Keys don't prevent children from re-rendering - rather, they allow for more efficient "reconciliation", by changing only the underlying DOM node that matches that key when needed. The child will still re-render in React regardless, but if the rendering of the child results in different markup, the key will be used to efficiently determine which DOM element(s) corresponding to that key needs to be changed, rather than changing everything.

If you want to prevent re-rendering, that requires a different tool - React.memo often works for individual components. For dynamic lists like these though, you can put the component into state.

const { useState } = React;
const data = () => {
  let result = [];
  for (let i = 0; i < 10; i++) {
    result.push({
      id: Math.random(),
      value: i + 1
    });
  }
  return result;
};

function Item({ item }) {
  console.log("render");
  return (
    <li>
      <label>{item.value}</label>
    </li>
  );
}

function App() {
  const [items, setItems] = useState(() => data().map(dataObj => <Item item={dataObj} />));
  function handleAdd(e) {
    setItems([
      ...items,
      <Item item={{
        id: Math.random(),
        value: items.length + 1
      }} />
    ]);
  }
  return (
    <div className="App">
      <button onClick={handleAdd}>Add</button>
      {items}
    </div>
  );
}

ReactDOM.render(<App />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

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