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

375
Vistas
React: What is the difference between const newArray = oldArray and const newArray = [...oldArray]

I have a piece of code in React, where if I use newArray = [... oldArray] it works, but if I use newArray = oldArray it doesn't. Can you tell me why?

Below the code. It's about adding a todo list when you click the Add button. So far it works. Then when you click on an item on the todo list, it should change class and become 'highlighted'. If I use newArray = oldArray, the state is correctly updated but then nothing changes on the screen:

const Todo = () => {
  const [item, setItem] = useState('');
  const [itemList, setItemList] = useState([]);

  const onChange = (e) => {
    setItem(e.target.value);
  };

  const handleClick = () => {
    setItemList((prevState) => [
      ...prevState,
      { id: Math.random(), item: item, isDone: false },
    ]);
    setItem('');
  };

  const handleItemClass = (i) => {
    const updatedItemList = itemList;
    updatedItemList[i].isDone = !updatedItemList[i].isDone;
    setItemList(updatedItemList);
  };

  return (
    <React.Fragment>
      <div>
        <input type="text" onChange={onChange} value={item} />
        <button onClick={handleClick}>Add</button>
      </div>
      <p>
        {itemList.filter((el) => el.isDone).length} completed from{' '}
        {itemList.length}
      </p>
      <ul>
        {itemList.map((itemCurrent, i) => (
          <li
            key={itemCurrent.id}
            onClick={() => handleItemClass(i)}
            className={itemCurrent.isDone ? 'is-done' : 'not-done'}
          >
            {itemCurrent.item}
          </li>
        ))}
      </ul>
    </React.Fragment>
  );
};
export default Todo;

render(<Todo />, document.getElementById('root'));
about 4 years ago · Juan Pablo Isaza
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