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

codebullet's avatar

Inertia Js and React Js > form callback is not triggered

Hi Artisans, Please I am facing an issue not sure if I missed something with react setup Please any heads-up will help


// Laravel 9.37
// React 18.2.0
// React-dom 18.2.0
// inertia react 0.8.1
// inertia js 0.11.0
// inertia-laravel 0.6.3

import React, {useEffect, useState} from 'react';
import {Head, useForm} from "@inertiajs/inertia-react"; 

const {data, setData, post, processing, errors, transform} = useForm({
    first_name: ' ',
    last_name:  ' ',
    title:  ' ',
    country_id: ' ',
    state_id: ' ',
    bio: ' ',
  })

function submit(e) {
    e.preventDefault()
    post(route('profile.update', {
    // preserveState: true, tried with use state still didn't work 
	// this doesn't work as well, it seems the page actually refreshes after posting the data 
      preserveScroll: true,
		
	//None of these works
      onSuccess: () => console.log('I am successful'),
      wasSuccess: () => console.log('This was successful'),
      recentlySuccessful: () => console.log('Recently successful'),
    }))
  }

Laravel Backend


     public function update(Request $request, Profile $profile) {
            $request->validate([
                'first_name' => 'required',
                'last_name' => 'required',
            ]);

            $profile->update([
                'first_name' => $request->input('first_name'),
                'last_name' => $request->input('last_name'),
                'title' => $request->input('title'),
                'bio' => $request->input('bio'),
                'country_id' => $request->input('country_id'),
                'state_id' => $request->input('state_id'),
			])

		return back()

Data will be updated, and the page component end will jump back to the top If I flash data it will be available in usePage().props not sure why form help is not working I have a similar setup with Vue 3 works fine but not sure what I missed in React

0 likes
3 replies
Sinnbeck's avatar

Looks correct. Can you show form as well? It sounds like you aren't submitting using inertia at all

Also check the network tab in the dev tools to make sure you get a 200 response.

codebullet's avatar

@Sinnbeck thank you for your reply, I am just surprised that it doesn't work out of the box from my end. with examples inertia website.


   <form className="bg-white w-full rounded-lg p-4 space-y-6"
                onSubmit={submit}
          >
        
            <div className="w-full flex flex-col lg:flex-row">
              <div className="w-full lg:w-1/3 px-3 py-1">
                <div className="">
                  <InputLabel forInput="first_name" className="mb-1">First Name </InputLabel>
                  <TextInput value={data.first_name} handleChange={e => setData('first_name', e.target.value)} required
                             name="first_name" id="first_name" placeholder="First Name"/>
                  <InputError message={errors.first_name} className="mt-2" />
                </div>

              </div>
				...... 
   			<div className="w-full flex justify-end mt-6 p-3">
              <PrimaryButton processing={processing} > 
                Update
              </PrimaryButton>
            </div>

everything looks good from network tab, data is updated in the database if I do

 return back()->with('message', 'Profile Updated');
	// I can get the message in usePage().props
//network tab screenshot => https://prnt.sc/y6a-Jr7Xm4nO

With Axios async I could achieve my goal, I just wanted to make use of inertia form

Sinnbeck's avatar

@codebullet yeah something seems off. I use react with inertia daily and have similar code, which works (except I rarely use the onSuccess. I use hooks).

I assume the processing button triggers as well (I assume it shows loading)

Please or to participate in this conversation.