Docs
API

API

createStore

const useStore = createStore<T>(initializer, {
  defaultDeps,
  intercept,
  onFirstSubscribe,
  onSubscribe,
  onUnsubscribe,
  onLastUnsubscribe,
});
 
// Inside component or custom hook
const state = useStore(selectDeps);

Parameters

Return Value

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

Return Value

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 response
    • staleTime Determine how long a response is categorized as fresh, defaults to 3 seconds
    • cacheTime Determine how long the query data should be stored in memory, defaults to 5 minutes
    • fetchOnMount 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 milliseconds
    • keepPreviousData Return the previous key's data until the current key's fetching finished
    • getNextPageParam Determine the value that should be used when getting next page
    • refetchInterval Auto refetching / polling interval (learn more)
    • onSuccess Triggered after fetch, refetch, & fetch next/prev page succeed
    • onError Triggered after fetch, refetch, & fetch next/prev page failed
    • onSettled Triggered after fetch, refetch, & fetch next/prev page (both succeed or failed)
    • ... same as useStores's

Return Value

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)
    • Return Value
      • state
  • Methods

    • ... same as useStore's