Skip to content

useQuery ​

Hook that defines the logic when query data

utilities
test coverage
Last changed: last month

Installation ​

Library
CLI
Manual
typescript
import { useQuery } from '@siberiacancode/reactuse';

Usage ​

typescript
const { data, isFetching, isLoading, isError, isSuccess, error, refetch, isRefetching, abort, aborted } = useQuery(() => fetch('url'));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
callback() => Promise<Data>-The callback function to be invoked
options.keys?DependencyList-The dependencies for the hook
options.onSuccess?(data: Data) => void-The callback function to be invoked on success
options.onError?(error: Error) => void-The callback function to be invoked on error
options.select?UseQueryOptionsSelect<Data>-The select function to be invoked
options.initialData?Data | (() => Data)-The initial data for the hook
options.placeholderData?Data | (() => Data)-The placeholder data for the hook
options.refetchInterval?number-The refetch interval
options.retry?boolean | number-The retry count of requests

Returns

UseQueryReturn<Data>

Type declaration ​

typescript
import type { DependencyList } from 'react';

export interface UseQueryOptions<QueryData, Data> {
  /* The enabled state of the query */
  enabled?: boolean;
  /* The depends for the hook */
  keys?: DependencyList;
  /* The placeholder data for the hook */
  placeholderData?: (() => Data) | Data;
  /* The refetch interval */
  refetchInterval?: number;
  /* The retry count of requests */
  retry?: boolean | number;
  /* The retry delay of requests */
  retryDelay?: ((retry: number, error: Error) => number) | number;
  /* The callback function to be invoked on error */
  onError?: (error: Error) => void;
  /* The callback function to be invoked on success */
  onSuccess?: (data: Data) => void;
  /* The select function to be invoked */
  select?: (data: QueryData) => Data;
}

interface UseQueryCallbackParams {
  /* The depends for the hook */
  keys: DependencyList;
  /* The abort signal */
  signal: AbortSignal;
}

export interface UseQueryReturn<Data> {
  /* The abort function */
  abort: AbortController['abort'];
  /* The state of the query */
  data?: Data;
  /* The success state of the query */
  error?: Error;
  /* The error state of the query */
  isError: boolean;
  /* The fetching state of the query */
  isFetching: boolean;
  /* The loading state of the query */
  isLoading: boolean;
  /* The refetching state of the query */
  isRefetching: boolean;
  /* The success state of the query */
  isSuccess: boolean;
  /* The refetch function */
  refetch: () => void;
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
V
Vitaly Statsura
Vitaly Statsura
V
Vitaly Statsura
Vitaly Statsura
H
hywax
hywax

Released under the MIT License.