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

182
Views
setState didn't set the latest value of the input form

I using useState to make handleChange() func. to update my form value and sent to other component for further using. but when I console.log() the value from state to checked that what value are stored right now, I found that It store the previous one.

example

input form: "a" console.log: ""

input form: "ab" console.log: "a"

input form: "abc" console.log: "ab"

setState update the previous one not the latest one. So when I submit the form value will missing the latest value of the form

All of My handleChange() and <input> be like this:

  const [classDataValue, setclassDataValue] = useState({
    className: "",
    classType: "",
    ownerEmail: user,
    collEmail: "",
    periodList: [],
    PollList: [],
  });

 function handleClassChange(e) {
    //handle the change of the input
    const { name, value } = e.target;
    console.log(name, value);
    setclassDataValue((prev) => ({
      ...prev,
      [name]: value,
    }));
    console.log(classDataValue); //still show previous value after set the new one
  }


  return(
    <input
       required
       type="text"
       id="ClassName"
       name="ClassName"
       onChange={handleClassChange}
    ></input>

      //others Input form
  )

Once I found some solutions like use setState callback, but I tried it and not working for me. So, I don't know how to handle this problem and stuck for 2 days now. If anyone have any Idea or solution, It's gonna be help me a lot. thank you

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