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

304
Visualizações
Why is my React state not changing after the setState is called?

This problem only occurs when I run my React/Express app in development mode through a Webpack proxy. I am essentially looking to post some data to and retrieve some data from my Express server at /post. The request goes through successfully with a 200 response.

It has no problem going through the setState function, but if I print the state after it is still null despite what I set it to being a valid string. As such the data I am looking to display on screen when it is called is never displayed. As can be seen in the photo attached the data is all valid but the state ends up being set to 0.

Everything functions properly if I build it before running and do not get the response through a proxy.

Here is the entire repo and the relevant component:

  • Repo: https://github.com/The-Briel-Deal/Flight_Scanner_MUI
  • Component: https://github.com/The-Briel-Deal/Flight_Scanner_MUI/blob/main/src/components/ShowFlights.jsx
// First of all, I use state with a object as follows.
let [returnQuery, setReturnQuery] = React.useState({
    price: null,
    connections: null,
    leaves: null,
    returns: null,
    link: null
})

// Following I fetch from my express server.
async function postData() {
    const response = await fetch('/search', {
        method: "POST",
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(props.searchState)
    })
    return response.json();
}

// After this I set my state as this return value
postData().then((data) => {
    console.log(`data returned ${data.link}`)
    setReturnQuery({
            price: data.price,
            connections: data.connections,
            leaves: data.leaves,
            returns: data.returns,
            link: data.link
    })
    setTimeout(() => console.log(returnQuery), 5000)
})

What my console returns

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