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

254
Visualizações
Updating the useState values automatically in react js?

I have a usestate array object called mo, which gets the data from an rest api from get request. I use useEffect for getting the data.

 const [mo, UpdateMO] = useState([{}]);

I will be creating an item by post request and that newly created item is stored in tempArray. This newly created item is appended to the mo array object.

UpdateMO([...mo], tempArray);

I show all the values of mo in a table. It is getting updated only once a re-render or refresh is taking place. On refreshing I would be going back to the homepage. So, how can I refresh or re-render the mo so that on creating a new item it automatically re-renders the mo.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Setting state will be done asynchronously. When you want to update state, you can grab the previous values in the state to combine with the new values.

Setting state also triggers a re-render.

// initialize state with an empty array
const [mo, setMo] = useState([]);

// get newValues and update state
setMo((previousValues) => [...previousValues, newValues]);
about 4 years ago · Juan Pablo Isaza Relatório

0

Use redux for that to maintain data and update data

about 4 years ago · Juan Pablo Isaza 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