Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

demonz's avatar

useState doesn't update on submission

Hi guys, I am tired , I am trying to send the data using Inertia.put but it is updated on the UI but it doesn't update when sending it via Inertia.put . help please , any suggestion

0 likes
6 replies
demonz's avatar
const [interest, setInterest] = useState({
     course_id: 1,
     count: 0,
   });


const submitInterest = (e) => {
 setInterest({
     ...interest,
     count: interest.count+1,
  });
     e.preventDefault()
     Inertia.put('learnerinterest', interest)
 }
Sinnbeck's avatar

@demonz Be sure that you prefix urls with / to make them absolute

     Inertia.put('/learnerinterest', interest)

Is it the count that does not change or ?

demonz's avatar

@Sinnbeck count changes on the UI but when I send the data , dd($request->interest) shows count is 0 , it should be 1

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@demonz Yeah I explained why in the other post. Shart version here. They interest variable isnt updated until the next render, so it is still 0.

An example of a fix

const data = {
     ...interest,
     count: interest.count+1,
  }

 setInterest(data);
 e.preventDefault()
 Inertia.put('learnerinterest', {interest: data})
1 like
Sinnbeck's avatar

@demonz Happy to help :) It can be a bit hard to understand at first :) Think of it as react reading the file from the top down. When you set something with useState() it remembers you want to do this, and then sets it next time the file is read from the top (a new render). This ensures you dont accidentally change it mid render, and thereby getting wrong data displayed, or logic triggered

1 like

Please or to participate in this conversation.