API
createStore
const useStore = createStore<T>(initializer, {
defaultDeps,
intercept,
onFirstSubscribe,
onSubscribe,
onUnsubscribe,
onLastUnsubscribe,
});
// Inside component or custom hook
const state = useStore(selectDeps);Parameters
initializerA function that returns an initial stateoptions?(optional)defaultDepsSet default reactivity (learn more)interceptIntercept set stateonFirstSubscribeHandle first subscribe eventonSubscribeHandle subscribe eventonUnsubscribeHandle unsubscribe eventonLastUnsubscribeHandle last unsubscribe event
Return Value
-
useStore()- Parameters (optional)
selectDepsCustomize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
useStore.getGet stateuseStore.setSet stateuseStore.subscribeSubscribe the store stateuseStore.getSubscribersGet store subscribersuseStore.setDefaultValuesSet default/initial values from inside a component (must be called inside component before it gets any subscribers)useStore.WatchA 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
initializerA function that returns an initial single store stateoptions?(optional)hashKeyFnCustom function to transform the store key (object) into a hash string (learn more)onBeforeChangeKeyHandle change key passed to the hookonStoreInitializedHandle single store state initialized event- ... same as
useStore's
Return Value
-
useStores()- Parameters (optional)
storeKeyChoose which store state will be used (learn more)selectDepsCustomize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
useStores.getGet state of specific store (specific store key)useStores.getAllGet all stores stateuseStores.getAllWithSubscriberGet all stores state that has subscriberuseStores.setSet state of specific store (specific store key)useStores.setAllSet all stores stateuseStores.subscribeSubscribe to a store stateuseStores.getSubscribersGet subscribers of a store stateuseStores.getStoreGet a storeuseStores.getStoresGet all storesuseStores.setDefaultValuesSet default/initial values from inside a component (must be called inside component before it gets any subscribers)useStores.WatchA 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)selectTransform or select a portion of the raw responsestaleTimeDetermine how long a response is categorized as fresh, defaults to 3 secondscacheTimeDetermine how long the query data should be stored in memory, defaults to 5 minutesfetchOnMountShould we fetch/refetch when the component mounted?fetchOnWindowFocusShould we fetch/refetch when a window focus event triggered?fetchOnReconnectShould we fetch/refetch when a window online event triggered?enabledShould we fetch/refetch for any reason?retryMaximum error retry count (learn more)retryDelayError retry delay in millisecondskeepPreviousDataReturn the previous key's data until the current key's fetching finishedgetNextPageParamDetermine the value that should be used when getting next pagerefetchIntervalAuto refetching / polling interval (learn more)onSuccessTriggered after fetch, refetch, & fetch next/prev page succeedonErrorTriggered after fetch, refetch, & fetch next/prev page failedonSettledTriggered after fetch, refetch, & fetch next/prev page (both succeed or failed)- ... same as
useStores's
Return Value
-
useQuery()- Parameters (optional)
storeKeyChoose which query store state will be used (learn more)selectDepsCustomize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
useQuery.setInitialResponseSet default/initial response from inside a component (must be called inside component before it gets any subscribers)useQuery.resetReset query to empty state for all query keysuseQuery.resetSpecificKeyReset a specific query to empty stateuseQuery.invalidateInvalidate query for all query keys (learn more)useQuery.invalidateSpecificKeyInvalidate a specific queryuseQuery.optimisticUpdateOptimistic update (learn more)useQuery.suspendUse 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)onMutateHandle mutate event (right after the mutation function fired)onSuccessHandle success event (promise resolved)onErrorHandle error event (promise rejected)onSettledHandle success or error event- ... same as
useStore's
Return Value
-
useMutation()- Parameters (optional)
selectDepsCustomize when to re-render
- Return Value
state
- Parameters (optional)
-
Methods
- ... same as
useStore's
- ... same as