Creates a React context + hook pair bound to a specific FetchEngine instance.
Returns a [Provider, useHook] tuple — rename to whatever fits your domain.
Setup:
import { FetchEngine } from '@logosdx/fetch';
import { createFetchContext } from '@logosdx/react';
const api = new FetchEngine({ baseUrl: 'https://api.example.com' });
export const [ApiFetch, useApiFetch] = createFetchContext(api);
Wrap your app:
<ApiFetch>
<App />
</ApiFetch>
Queries — auto-fetch on mount, re-fetch when path/options change:
Returns { data, loading, error, response, refetch, cancel }.
data is the unwrapped T. response provides full FetchResponse
access (status, headers). error is a FetchError with .status,
.isCancelled(), .isTimeout(), etc.
function UserList() {
const { get } = useApiFetch();
const { data, loading, error, refetch } = get<User[]>('/users');
// Full response access for headers/status
const { response } = get<Post, { 'x-total': string }>('/posts');
// response?.headers['x-total'] is typed
if (loading) return <Spinner />;
if (error) return <Error message={error.message} status={error.status} />;
return <ul>{data?.map(u => <li key={u.id}>{u.name}</li>)}</ul>;
}
Mutations — fire on demand, track loading/result/error:
Returns { data, loading, error, response, mutate, reset, cancel, called }.
Starts idle (loading: false, called: false) until mutate() is called.
mutate() returns Promise<T> so you can await the result.
Rules:get, post, put, del, and patch call React hooks
internally, so they follow the same rules — call them at the top level
of your component, never conditionally or in loops.
Creates a React context + hook pair bound to a specific FetchEngine instance. Returns a
[Provider, useHook]tuple — rename to whatever fits your domain.Setup:
Wrap your app:
Queries — auto-fetch on mount, re-fetch when path/options change:
Returns
{ data, loading, error, response, refetch, cancel }.datais the unwrappedT.responseprovides fullFetchResponseaccess (status, headers).erroris aFetchErrorwith.status,.isCancelled(),.isTimeout(), etc.Mutations — fire on demand, track loading/result/error:
Returns
{ data, loading, error, response, mutate, reset, cancel, called }. Starts idle (loading: false,called: false) untilmutate()is called.mutate()returnsPromise<T>so you can await the result.Escape hatch —
instancegives raw access to the FetchEngine:Rules:
get,post,put,del, andpatchcall React hooks internally, so they follow the same rules — call them at the top level of your component, never conditionally or in loops.