Lets a component load one thing from the API the first time it's needed, and keep it for as long as its key holds, as the profile's lazy tabs do.
Returns what load resolves to for key, loaded the first time it is
enabled, and kept afterwards. A new key starts over and aborts the load of
the last one, as leaving the page does; an undefined key loads nothing. The
key must change with whatever the load depends on, the viewer included when
it is signed in. load must reject with an Error, as expectOk does,
whose message it shows.
Passed to useFetchState.
-
enabled: boolean
False until it should load, as the profile's tabs pass "this tab is open". Turning it off doesn't abort a load under way, so those tabs finish loading in the background. True by default.
-
initial: T
It holds the data already at hand for a new key, as a feed restored from
useFeedCache, which starts loaded and isn't loaded again. It is read only when the key changes.
Returned by useFetchState.
-
reload: () => void
Loads the key's data again, and replaces the data loaded with it, as the live pages do on
useOnReconnect. Until it arrives, the data shown stays, and so it does if the load fails. It does nothing until loaded. -
setData: (fn: (data: T) => T) => void
Edits the loaded data, as when a next page comes in. It does nothing until loaded.
- state: FetchState<T>
Held by useFetchState, and passed on to ListState.
Usage
import * as mod from "src/hooks/shared/useFetchState.ts";