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

104
Visualizações
Updating a single field in object Javascript

I need to change state of specific fields in problems object:

  const [problems, setProblems] = useState({
    first_name: false,
    last_name: false,
    city: false,
    email: false,
    password: false,
  });

I tried something like this below, but it updates for me just the last value of error Object.keys(error.response.data) list:

.catch((error) => {
        Object.keys(error.response.data).map((err) => {
          setProblems({ ...problems, [err]: true });
        });
      });

error is an object that has keys that have the same names as keys in problems object. How can I do this correctly that all fields will be updated?

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

0

It only updates the state with the last state value is because of closure over the state.

State is constant within a particular render of a component; in order to see the updated state, component has to re-render.

Since you are calling setProblems() multiple times within the same render:

setProblems({ ...problems, [err]: true });

problems in the above statement refers to the same state that was in-effect when Object.keys(...).map(...) was called. In short, you are spreading the same state each time setProblems is called.

Solution

What you need to do is:

  1. First create the object that you want to save in the state

    const newState = { ...problems };
    
    Object.keys(error.response.data)
          .forEach((err) => newState[err] = true);
    
  2. Call the state setter function

    setProblems(newState);
    
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

.catch((error) => {
    const newProblems = {...problems};

    Object.keys(error.response.data).forEach((err) => {
        newProblems[err] = true;
    });

    setProblems(newProblems);
});

I have not actually tested your original code, but what I am guessing is that since setState's setter is not synchronous, when you call it in a loop you are only applying the last call.

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