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

demonz's avatar

how I can send the updated state and not just only rendering

Hi guys , I am trying to send the updated state but it's always send the original one , count should be 1 not 0.

const [interest, setInterest] = useState({
     course_id: 1,
     count: 0,
   });


const submitInterest = (e) => {
 setInterest({
     ...interest,
     count: interest.count+1,
  });
     e.preventDefault()
     Inertia.put('learnerinterest', interest)
 }
0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The count will be sent as 0 to the the put endpoint, as the setState has not been run yet. So interest isnt actually updated until the next render cycle

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

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

@demonz I just gave you an example of a fix? We send the changed data to inertia

1 like

Please or to participate in this conversation.