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

171
Visualizações
How to make a stateful array with elements updating their own state individually in React?
import { useEffect, useState } from "react";
import { v4 as uuidv4 } from 'uuid';

interface Item {
  id: string,
  value: number
}

function App() {
  const [list, setList] = useState<Item[]>([]);

  useEffect(() => {
    console.log(list);
  }, [list])

  function createItem() {
    const id = uuidv4();
    setList([...list, { id: id, value: 1 }]);
    return id;
  }

  function updateItem(index: number, value: number) {
    var newArray = [...list];
    newArray[index].value = value;
    setList(newArray);
  }

  async function process() {
    const id = createItem()
    const index = list.findIndex(item => item.id === id);

    // Error starts here because list is empty (?) 

    await new Promise((resolve) => setTimeout(resolve, Math.floor(Math.random() * 10000)));
    updateItem(index, 2);
    await new Promise((resolve) => setTimeout(resolve, Math.floor(Math.random() * 10000)));
    updateItem(index, 3)
    await new Promise((resolve) => setTimeout(resolve, Math.floor(Math.random() * 10000)));
    updateItem(index, 4)
    await new Promise((resolve) => setTimeout(resolve, Math.floor(Math.random() * 10000)));
    updateItem(index, 5)
    await new Promise((resolve) => setTimeout(resolve, Math.floor(Math.random() * 10000)));
  }

  return (
    <div className="App">
      <button onClick={() => {
        process();
      }}>Create</button>
      {list.map((item) => {
        return <p key={item.id}>{item.value}</p>
      })}
    </div>
  );
}

export default App;

I am trying to make something like a stateful array but all array elements should update their own state individually.

Assume: User clicks create, a new number appears on screen starting from 1. It increments itself every x seconds. When user clicks Create again, same applies for newly created number but old number should also continue incrementing itself. Assume user clicked Create 100 times, all numbers should manage their own state and I should show it in screen.

But when I use code above it gives error just after trying to find index. There was a time it was working but then items were overriding each other's state because of [...list, item]. It is probably because previously created ones doesn't know about new ones.

I am totally confused. I just want to have a stateful array where all elements individually updating their own state.

One workaround is creating a new component for Item and having it store/update it's own data but I need to do this without creating a new component.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I had to refactor your code mainly because of following reasons.

Here:

const id = createItem()
const index = list.findIndex(item => item.id === id);

When you append item to list inside createItem, it is not immediately available on list where you are trying to do findIndex.

Also everywhere you are using setState now it is better to use functional form, to get access to previous state.

Check the code:

export default function App() {
  const [list, setList] = React.useState([]);

  React.useEffect(() => {
    console.log(list);
  }, [list]);

  function createItem() {
    const id = uuidv4();
    setList((ps) => [...ps, { id, value: 1 }]);
    return id;
  }

  function updateItem(id, value) {
    setList((ps) => ps.map((x) => (x.id === id ? { ...x, value } : x)));
  }

  async function process() {
    const id = createItem();

    for (let i = 2; i <= 5; i++) {
      await new Promise((resolve) =>
        setTimeout(resolve, Math.floor(Math.random() * 10000))
      );
      updateItem(id, i);
    }
  }

  return (
    <div className="App">
      <button
        onClick={() => {
          process();
        }}
      >
        Create
      </button>
      {list.map((item) => {
        return <p key={item.id}>{item.value}</p>;
      })}
    </div>
  );
}
about 4 years ago · Santiago Trujillo 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