Skip to content

useHttp ​

For JSON endpoints that aren't Inertia pages, useHttp has the same fields, errors and states as useForm, but the request is plain HTTP, so the page stays put.

ts
search = useHttp({ title: '' });

lookUp = async () => {
  const matches = await this.search.get('/api/ideas/similar'); // also in this.search.response
};

GET data goes in the query string. Other methods send JSON, or FormData when there are files. A 422 fills errors and resolves to undefined; any other failure rejects.

It takes onSuccess, onError, onHttpException, onNetworkError and optimistic (rolled back on failure), and with useHttp(method, url, data), Precognition. With TypeScript, useHttp<Fields, Response> types both.

The demo's newsletter box, on the home page, posts to /api/subscribers with useHttp, and the New idea page looks up similar ideas as you type. See the home page source, or try it ↗ .

Released under the MIT License.