Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda