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

141
Visualizações
Is it possible to access variable set by hooks inside callback function

I am using hooks to set varibale as such:

const [test1, setTest1] = useState('')
const [test2, setTest2] = useState('')

The value of test 1 and test2 are set after userinput onChnage={(e)=>setTest1(e.target.value)} and onChnage={(e)=>setTest2(e.target.value)}

Now is it possible to access the value of test1 and test2 in callback function? I have function that gets called on form submit button click as below but I am getting test1 and test2 as undefined.

const onHandleSubmit = useCallback( (event) => {
    console.log(test1)
    console.log(test2)
    let data = {
       test1 : test1,
       test2 : test2
    }
    dispatch({type: 'send/data', data:data});
},[]);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to write the variables used in the callback into the observations array as shown below:

const onHandleSubmit = useCallback( (event) => {
    console.log(test1)
    console.log(test2)
    let data = {
       test1,
       test2
    }
    dispatch({type: 'send/data', data:data});
},[test1, test2]); // <====

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