API
createStore
const useStore = createStore<T>(initializer, {
defaultDeps,
intercept,
onFirstSubscribe,
onSubscribe,
onUnsubscribe,
onLastUnsubscribe,
});
// Inside component or custom hook
const state = useStore(selectDeps);
Parameters
initializer
A function that returns an initial stateoptions?
(optional)defaultDeps
Set default reactivity (learn more)intercept
Intercept set stateonFirstSubscribe
Handle first subscribe eventonSubscribe
Handle subscribe eventonUnsubscribe
Handle unsubscribe eventonLastUnsubscribe
Handle last unsubscribe event
Return Value
-
useStore()
- Parameters (optional)
selectDeps
Customize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
useStore.get
Get stateuseStore.set
Set stateuseStore.subscribe
Subscribe the store stateuseStore.getSubscribers
Get store subscribersuseStore.setDefaultValues
Set default/initial values from inside a component (must be called inside component before it gets any subscribers)useStore.Watch
A helper component that will watch the state changes (learn more)
createStores
const useStores = createStores<TKey, T>(initializer, {
...options, // Same as createStore options
hashKeyFn,
onBeforeChangeKey,
onStoreInitialized,
});
// Inside component or custom hook
const state = useStores(storeKey, selectDeps);
const state = useStores(selectDeps);
Parameters
initializer
A function that returns an initial single store stateoptions?
(optional)hashKeyFn
Custom function to transform the store key (object) into a hash string (learn more)onBeforeChangeKey
Handle change key passed to the hookonStoreInitialized
Handle single store state initialized event- ... same as
useStore
's
Return Value
-
useStores()
- Parameters (optional)
storeKey
Choose which store state will be used (learn more)selectDeps
Customize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
useStores.get
Get state of specific store (specific store key)useStores.getAll
Get all stores stateuseStores.getAllWithSubscriber
Get all stores state that has subscriberuseStores.set
Set state of specific store (specific store key)useStores.setAll
Set all stores stateuseStores.subscribe
Subscribe to a store stateuseStores.getSubscribers
Get subscribers of a store stateuseStores.getStore
Get a storeuseStores.getStores
Get all storesuseStores.setDefaultValues
Set default/initial values from inside a component (must be called inside component before it gets any subscribers)useStores.Watch
A helper component that will watch the state changes (learn more)
createQuery
const useQuery = createQuery<TKey, TResponse, TData, TError>(queryFn, {
...options, // Same as createStores options
select,
staleTime,
cacheTime,
fetchOnMount,
fetchOnWindowFocus,
enabled,
retry,
retryDelay,
keepPreviousData,
getNextPageParam,
refetchInterval,
onSuccess,
onError,
onSettled,
});
// Inside component or custom hook
const state = useQuery(storeKey, selectDeps);
const state = useQuery(selectDeps);
Parameters
-
queryFn
options?
(optional)select
Transform or select a portion of the raw responsestaleTime
Determine how long a response is categorized as fresh, defaults to 3 secondscacheTime
Determine how long the query data should be stored in memory, defaults to 5 minutesfetchOnMount
Should we fetch/refetch when the component mounted?fetchOnWindowFocus
Should we fetch/refetch when a window focus event triggered?fetchOnReconnect
Should we fetch/refetch when a window online event triggered?enabled
Should we fetch/refetch for any reason?retry
Maximum error retry count (learn more)retryDelay
Error retry delay in millisecondskeepPreviousData
Return the previous key's data until the current key's fetching finishedgetNextPageParam
Determine the value that should be used when getting next pagerefetchInterval
Auto refetching / polling interval (learn more)onSuccess
Triggered after fetch, refetch, & fetch next/prev page succeedonError
Triggered after fetch, refetch, & fetch next/prev page failedonSettled
Triggered after fetch, refetch, & fetch next/prev page (both succeed or failed)- ... same as
useStores
's
Return Value
-
useQuery()
- Parameters (optional)
storeKey
Choose which query store state will be used (learn more)selectDeps
Customize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
useQuery.setInitialResponse
Set default/initial response from inside a component (must be called inside component before it gets any subscribers)useQuery.reset
Reset query to empty state for all query keysuseQuery.resetSpecificKey
Reset a specific query to empty stateuseQuery.invalidate
Invalidate query for all query keys (learn more)useQuery.invalidateSpecificKey
Invalidate a specific queryuseQuery.optimisticUpdate
Optimistic update (learn more)useQuery.suspend
Use query with suspense mode (learn more)- ... same as
useStores
's
createMutation
const useMutation = createMutation<T>(initializer, {
...options, // Same as createStore options
onMutate,
onSuccess,
onError,
onSettled,
});
// Inside component or custom hook
const state = useMutation(selectDeps);
Parameters
-
mutationFn
options?
(optional)onMutate
Handle mutate event (right after the mutation function fired)onSuccess
Handle success event (promise resolved)onError
Handle error event (promise rejected)onSettled
Handle success or error event- ... same as
useStore
's
Return Value
-
useMutation()
- Parameters (optional)
selectDeps
Customize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
- ... same as
useStore
's
- ... same as