useHttp hook provides the same developer experience as useForm, but for standalone HTTP requests.
Basic Usage
TheuseHttp hook accepts initial data and returns reactive state along with methods for making HTTP requests.
useHttp requests do not trigger page navigation or interact with Inertia’s page lifecycle. They are plain HTTP requests that return JSON responses.
Submitting Data
The hook providesget, post, put, patch, and delete convenience methods. A generic submit method is also available for dynamic HTTP methods.
Promise that resolves with the parsed JSON response data.
Reactive State
TheuseHttp hook exposes the same reactive properties as useForm:
| Property | Type | Description |
|---|---|---|
errors | object | Validation errors keyed by field name |
hasErrors | boolean | Whether validation errors exist |
processing | boolean | Whether a request is in progress |
progress | object | null | Upload progress with percentage and total |
wasSuccessful | boolean | Whether the last request was successful |
recentlySuccessful | boolean | true for two seconds after a successful request |
isDirty | boolean | Whether the data differs from its defaults |
Validation Errors
When a request returns a422 status code, the hook automatically parses validation errors and makes them available through the errors property.
Displaying All Errors
By default, validation errors are simplified to the first error message for each field. You may chainwithAllErrors() to receive all error messages as arrays, which is useful for fields with multiple validation rules.
useForm helper and the <Form> component.
File Uploads
When the data includes files, the hook automatically sends the request asmultipart/form-data. Upload progress is available through the progress property.
Cancelling Requests
You may cancel an in-progress request using thecancel() method.
Optimistic Updates
TheuseHttp hook supports optimistic updates via the optimistic() method. The callback receives the current data and should return a partial update to apply immediately.
Event Callbacks
Each submit method accepts an options object with lifecycle callbacks:false from onBefore to cancel the request.
Precognition
TheuseHttp hook supports Laravel Precognition for real-time validation. Enable it by chaining withPrecognition() with the HTTP method and validation endpoint.
validate(), touch(), touched(), valid(), and invalid() methods become available, working identically to form precognition.
History State
You may persist data and errors in browser history state by providing a remember key as the first argument.dontRemember() method.