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

318
Visualizações
How to fix issue with state not equal to current state

I am calling a function that uses state but the state that is being used in function is old state even though if you use the react debugger on chrome it displays new state.

const [somevalue,setsomevalue] = useState("newstate")

    function myfunction()
    {
       console.log(somevalue)
    }

myfunction()

output:

oldstate

I was thinking of using forceUpdate() but I am using classless component.

const CauseCodeChangeHandler = (e) => {
  
      setSelectedCauseCode(e.target.value);
  };

const serQtyChangeHandler = (e) =>
  {
    
    if (SelectedCauseCode == "CUS")
    {
       
     
    }
    else if (SelectedCauseCode == "OPS")
    {
    }
    else if (SelectedCauseCode == 'OOW')
    {
      
    }
  }

my component:

<ServiceSelectInput changeHandler={CauseCodeChangeHandler}/> 

  

It is using old CauseCode not one that I just selected.

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

0

This is the stale closure problem and is something that useCallback is designed to address:

import {useCallback, useState} from 'react';

function Component () {
  const [someValue, setSomeValue] = useState('newstate');

  // This will make sure that `myFunction` is recreated
  // every time that `someValue` changes
  const myFunction = useCallback(() => console.log(someValue), [someValue]);

  // ...rest of component
}

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