Router Visits
You may chain theoptimistic() method before any router visit. The callback receives the current page props and should return a partial update to apply immediately.
Form Component
The<Form> component supports optimistic updates via the optimistic prop. Since the component manages input data internally, the form data is provided as a second callback argument.
Form Helper
TheuseForm helper also supports optimistic updates via the same optimistic() method.
HTTP Requests
TheuseHttp hook supports optimistic updates as well. Since HTTP requests don’t interact with Inertia’s page props, the optimistic callback receives and updates the form’s own data. On failure, the form data is reverted to its pre-request state.
How It Works
When an optimistic update is applied:- The returned props are compared against the current page props, and only the keys that actually changed are snapshotted
- The callback’s return value is merged into the current data
- The request is sent to the server
- On success, the server’s response replaces the optimistic data
- On failure, only the snapshotted keys are reverted, rolling back the optimistic changes
Automatic Rollback
Optimistic state is automatically reverted in several scenarios:- Validation errors (422): The optimistic state is reverted and the validation errors are preserved
- Server errors: When the request fails for any other reason, the original state is restored
- Interrupted visits: When a new visit interrupts an in-flight request, the previous optimistic state is restored before the new optimistic update is applied
Concurrent Updates
Multiple optimistic requests may be in flight at the same time. Inertia tracks which props each optimistic update touched, and server responses will not overwrite a prop until the last optimistic request that modified it has resolved.Inline Option
You may also pass the optimistic callback directly in the visit options instead of chaining.useHttp submit methods as well.