Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!