react-use

  • Version 17.5.0
  • Published
  • 539 kB
  • 14 dependencies
  • Unlicense license

Install

npm i react-use
yarn add react-use
pnpm add react-use

Overview

Collection of React Hooks

Index

Variables

Functions

Variables

variable useBattery

const useBattery: () => UseBatteryState;

    variable useIsomorphicLayoutEffect

    const useIsomorphicLayoutEffect: any;

      variable useMeasure

      const useMeasure: <E extends Element = Element>() => UseMeasureResult<E>;

        variable usePromise

        const usePromise: UsePromise;

          variable useSearchParam

          const useSearchParam: UseQueryParam;

            variable useTitle

            const useTitle: (title: string, options?: UseTitleOptions) => void;

              variable useUpdateEffect

              const useUpdateEffect: any;

                variable useVibrate

                const useVibrate: (
                enabled?: boolean,
                pattern?: VibrationPattern,
                loop?: boolean
                ) => void;

                  Functions

                  function createBreakpoint

                  createBreakpoint: (breakpoints?: { [name: string]: number }) => () => string;

                    function createGlobalState

                    createGlobalState: {
                    <S = any>(initialState: IHookStateInitAction<S>): () => [
                    S,
                    (state: IHookStateSetAction<S>) => void
                    ];
                    <S = undefined>(): () => [S, (state: IHookStateSetAction<S>) => void];
                    };

                      function createMemo

                      createMemo: <T extends (...args: any) => any>(
                      fn: T
                      ) => (...args: Parameters<T>) => ReturnType<T>;

                        function createReducer

                        createReducer: <Action, State>(
                        ...middlewares: Middleware<Action, State>[]
                        ) => (
                        reducer: (state: State, action: Action) => State,
                        initialState: State,
                        initializer?: (value: State) => State
                        ) => [State, Dispatch<Action>];

                          function createReducerContext

                          createReducerContext: <R extends any>(
                          reducer: R,
                          defaultInitialState: any
                          ) => readonly [
                          () => [any, any],
                          ({
                          children,
                          initialState,
                          }: {
                          children?: React.ReactNode;
                          initialState?: any;
                          }) => any,
                          any
                          ];

                            function createStateContext

                            createStateContext: <T>(
                            defaultInitialValue: T
                            ) => readonly [
                            () => [T, any],
                            ({
                            children,
                            initialValue,
                            }: {
                            children?: React.ReactNode;
                            initialValue?: T;
                            }) => any,
                            any
                            ];

                              function ensuredForwardRef

                              ensuredForwardRef: <T, P = {}>(
                              Component: RefForwardingComponent<T, P>
                              ) => ForwardRefExoticComponent<any>;

                                function useAsync

                                useAsync: <T extends FunctionReturningPromise>(
                                fn: T,
                                deps?: DependencyList
                                ) => import('./useAsyncFn').AsyncState<
                                import('./misc/types').PromiseType<ReturnType<T>>
                                >;

                                  function useAsyncFn

                                  useAsyncFn: <T extends FunctionReturningPromise>(
                                  fn: T,
                                  deps?: DependencyList,
                                  initialState?: StateFromFunctionReturningPromise<T>
                                  ) => AsyncFnReturn<T>;

                                    function useAsyncRetry

                                    useAsyncRetry: <T>(
                                    fn: () => Promise<T>,
                                    deps?: DependencyList
                                    ) =>
                                    | { retry: () => void; loading: boolean; error?: undefined; value?: undefined }
                                    | { retry: () => void; loading: false; error: Error; value?: undefined }
                                    | { retry: () => void; loading: true; error?: Error | undefined; value?: T }
                                    | { retry: () => void; loading: false; error?: undefined; value: T };

                                      function useAudio

                                      useAudio: (
                                      elOrProps: any
                                      ) => readonly [
                                      any,
                                      HTMLMediaState,
                                      {
                                      play: () => Promise<void> | undefined;
                                      pause: () => void;
                                      seek: (time: number) => void;
                                      volume: (volume: number) => void;
                                      mute: () => void;
                                      unmute: () => void;
                                      },
                                      any
                                      ];

                                        function useBeforeUnload

                                        useBeforeUnload: (
                                        enabled?: boolean | (() => boolean),
                                        message?: string | undefined
                                        ) => void;

                                          function useBoolean

                                          useBoolean: (initialValue: boolean) => [boolean, (nextValue?: any) => void];

                                            function useClickAway

                                            useClickAway: <E extends Event = Event>(
                                            ref: RefObject<HTMLElement>,
                                            onClickAway: (event: E) => void,
                                            events?: string[]
                                            ) => void;

                                              function useCookie

                                              useCookie: (
                                              cookieName: string
                                              ) => [
                                              string,
                                              (newValue: string, options?: Cookies.CookieAttributes | undefined) => void,
                                              () => void
                                              ];

                                                function useCopyToClipboard

                                                useCopyToClipboard: () => [CopyToClipboardState, (value: string) => void];

                                                  function useCounter

                                                  useCounter: (
                                                  initialValue?: IHookStateInitAction<number>,
                                                  max?: number | null,
                                                  min?: number | null
                                                  ) => [number, CounterActions];

                                                    function useCss

                                                    useCss: (css: object) => string;

                                                      function useCustomCompareEffect

                                                      useCustomCompareEffect: <TDeps extends DependencyList>(
                                                      effect: EffectCallback,
                                                      deps: TDeps,
                                                      depsEqual: DepsEqualFnType<TDeps>
                                                      ) => void;

                                                        function useDebounce

                                                        useDebounce: (
                                                        fn: Function,
                                                        ms?: number,
                                                        deps?: DependencyList
                                                        ) => UseDebounceReturn;

                                                          function useDeepCompareEffect

                                                          useDeepCompareEffect: (effect: EffectCallback, deps: DependencyList) => void;

                                                            function useDefault

                                                            useDefault: <TStateType>(
                                                            defaultValue: TStateType,
                                                            initialValue: TStateType | (() => TStateType)
                                                            ) => readonly [TStateType, any];

                                                              function useDrop

                                                              useDrop: (options?: DropAreaOptions, args?: never[]) => DropAreaState;

                                                                function useDropArea

                                                                useDropArea: (options?: DropAreaOptions) => [DropAreaBond, DropAreaState];

                                                                  function useEffectOnce

                                                                  useEffectOnce: (effect: EffectCallback) => void;

                                                                    function useEnsuredForwardedRef

                                                                    useEnsuredForwardedRef: <T>(
                                                                    forwardedRef: MutableRefObject<T>
                                                                    ) => MutableRefObject<T>;

                                                                      function useError

                                                                      useError: () => (err: Error) => void;

                                                                        function useEvent

                                                                        useEvent: <T extends UseEventTarget>(
                                                                        name: Parameters<AddEventListener<T>>[0],
                                                                        handler?: Parameters<AddEventListener<T>>[1],
                                                                        target?: T | Window,
                                                                        options?: UseEventOptions<T>
                                                                        ) => void;

                                                                          function useFavicon

                                                                          useFavicon: (href: string) => void;

                                                                            function useFirstMountState

                                                                            useFirstMountState: () => boolean;

                                                                              function useFullscreen

                                                                              useFullscreen: (
                                                                              ref: RefObject<Element>,
                                                                              enabled: boolean,
                                                                              options?: FullScreenOptions
                                                                              ) => boolean;

                                                                                function useGeolocation

                                                                                useGeolocation: (
                                                                                options?: PositionOptions | undefined
                                                                                ) => GeoLocationSensorState;

                                                                                  function useGetSet

                                                                                  useGetSet: <S>(
                                                                                  initialState: IHookStateInitAction<S>
                                                                                  ) => [get: () => S, set: Dispatch<IHookStateSetAction<S>>];

                                                                                    function useGetSetState

                                                                                    useGetSetState: <T extends object>(
                                                                                    initialState?: T
                                                                                    ) => [() => T, (patch: Partial<T>) => void];

                                                                                      function useHarmonicIntervalFn

                                                                                      useHarmonicIntervalFn: (fn: Function, delay?: number | null) => void;

                                                                                        function useHash

                                                                                        useHash: () => readonly [string, (newHash: string) => void];
                                                                                        • read and write url hash, response to url hash change

                                                                                        function useHover

                                                                                        useHover: (element: Element) => [React.ReactElement<any>, boolean];

                                                                                          function useHoverDirty

                                                                                          useHoverDirty: (ref: RefObject<Element>, enabled?: boolean) => boolean;

                                                                                            function useIdle

                                                                                            useIdle: (ms?: number, initialState?: boolean, events?: string[]) => boolean;

                                                                                              function useIntersection

                                                                                              useIntersection: (
                                                                                              ref: RefObject<HTMLElement>,
                                                                                              options: IntersectionObserverInit
                                                                                              ) => IntersectionObserverEntry | null;

                                                                                                function useInterval

                                                                                                useInterval: (callback: Function, delay?: number | null | undefined) => void;

                                                                                                  function useKey

                                                                                                  useKey: <T extends UseEventTarget>(
                                                                                                  key: KeyFilter,
                                                                                                  fn?: Handler,
                                                                                                  opts?: UseKeyOptions<T>,
                                                                                                  deps?: DependencyList
                                                                                                  ) => void;

                                                                                                    function useKeyPress

                                                                                                    useKeyPress: (keyFilter: KeyFilter) => [boolean, KeyboardEvent | null];

                                                                                                      function useKeyPressEvent

                                                                                                      useKeyPressEvent: (
                                                                                                      key: string | KeyFilter,
                                                                                                      keydown?: Handler | null | undefined,
                                                                                                      keyup?: Handler | null | undefined,
                                                                                                      useKeyPress?: (keyFilter: KeyFilter) => [boolean, KeyboardEvent | null]
                                                                                                      ) => void;

                                                                                                        function useLatest

                                                                                                        useLatest: <T>(value: T) => { readonly current: T };

                                                                                                          function useLifecycles

                                                                                                          useLifecycles: (mount: any, unmount?: any) => void;

                                                                                                            function useList

                                                                                                            useList: <T>(initialList?: IHookStateInitAction<T[]>) => [T[], ListActions<T>];

                                                                                                              function useLocalStorage

                                                                                                              useLocalStorage: <T>(
                                                                                                              key: string,
                                                                                                              initialValue?: T,
                                                                                                              options?:
                                                                                                              | { raw: true }
                                                                                                              | {
                                                                                                              raw: false;
                                                                                                              serializer: (value: T) => string;
                                                                                                              deserializer: (value: string) => T;
                                                                                                              }
                                                                                                              ) => [T, Dispatch<SetStateAction<T>>, () => void];

                                                                                                                function useLocation

                                                                                                                useLocation: () => LocationSensorState;

                                                                                                                  function useLockBodyScroll

                                                                                                                  useLockBodyScroll: (
                                                                                                                  _locked?: boolean,
                                                                                                                  _elementRef?: RefObject<HTMLElement> | undefined
                                                                                                                  ) => void;

                                                                                                                    function useLogger

                                                                                                                    useLogger: (componentName: string, ...rest: any[]) => void;

                                                                                                                      function useLongPress

                                                                                                                      useLongPress: (
                                                                                                                      callback: (e: TouchEvent | MouseEvent) => void,
                                                                                                                      { isPreventDefault, delay }?: Options
                                                                                                                      ) => {
                                                                                                                      readonly onMouseDown: (e: any) => void;
                                                                                                                      readonly onTouchStart: (e: any) => void;
                                                                                                                      readonly onMouseUp: () => void;
                                                                                                                      readonly onMouseLeave: () => void;
                                                                                                                      readonly onTouchEnd: () => void;
                                                                                                                      };

                                                                                                                        function useMap

                                                                                                                        useMap: <T extends object = any>(initialMap?: T) => [T, Actions<T>];

                                                                                                                          function useMedia

                                                                                                                          useMedia: (query: string, defaultState?: boolean | undefined) => boolean;

                                                                                                                            function useMediaDevices

                                                                                                                            useMediaDevices: () => {};

                                                                                                                              function useMediatedState

                                                                                                                              useMediatedState: {
                                                                                                                              <S = undefined>(
                                                                                                                              mediator: StateMediator<S | undefined>
                                                                                                                              ): UseMediatedStateReturn<S>;
                                                                                                                              <S = any>(
                                                                                                                              mediator: StateMediator<S>,
                                                                                                                              initialState: S
                                                                                                                              ): UseMediatedStateReturn<S>;
                                                                                                                              };

                                                                                                                                function useMethods

                                                                                                                                useMethods: <M, T>(
                                                                                                                                createMethods: CreateMethods<M, T>,
                                                                                                                                initialState: T
                                                                                                                                ) => [T, WrappedMethods<M>];

                                                                                                                                  function useMotion

                                                                                                                                  useMotion: (initialState?: MotionSensorState) => MotionSensorState;

                                                                                                                                    function useMount

                                                                                                                                    useMount: (fn: () => void) => void;

                                                                                                                                      function useMountedState

                                                                                                                                      useMountedState: () => () => boolean;

                                                                                                                                        function useMouse

                                                                                                                                        useMouse: (ref: RefObject<Element>) => State;

                                                                                                                                          function useMouseHovered

                                                                                                                                          useMouseHovered: (
                                                                                                                                          ref: RefObject<Element>,
                                                                                                                                          options?: UseMouseHoveredOptions
                                                                                                                                          ) => State;

                                                                                                                                            function useMouseWheel

                                                                                                                                            useMouseWheel: () => number;

                                                                                                                                              function useMultiStateValidator

                                                                                                                                              useMultiStateValidator: <
                                                                                                                                              V extends ValidityState,
                                                                                                                                              S extends MultiStateValidatorStates
                                                                                                                                              >(
                                                                                                                                              states: S,
                                                                                                                                              validator: MultiStateValidator<V, S>,
                                                                                                                                              initialValidity?: V
                                                                                                                                              ) => UseStateValidatorReturn<V>;

                                                                                                                                                function useNetworkState

                                                                                                                                                useNetworkState: (
                                                                                                                                                initialState?: IHookStateInitAction<IUseNetworkState>
                                                                                                                                                ) => IUseNetworkState;

                                                                                                                                                  function useNumber

                                                                                                                                                  useNumber: (
                                                                                                                                                  initialValue?: IHookStateInitAction<number>,
                                                                                                                                                  max?: number | null,
                                                                                                                                                  min?: number | null
                                                                                                                                                  ) => [number, CounterActions];

                                                                                                                                                    function useObservable

                                                                                                                                                    useObservable: {
                                                                                                                                                    <T>(observable$: Observable<T>): T | undefined;
                                                                                                                                                    <T>(observable$: Observable<T>, initialValue: T): T;
                                                                                                                                                    };

                                                                                                                                                      function useOrientation

                                                                                                                                                      useOrientation: (initialState?: OrientationState) => OrientationState;

                                                                                                                                                        function usePageLeave

                                                                                                                                                        usePageLeave: (onPageLeave: any, args?: never[]) => void;

                                                                                                                                                          function usePermission

                                                                                                                                                          usePermission: (permissionDesc: IPermissionDescriptor) => IState;

                                                                                                                                                            function usePinchZoom

                                                                                                                                                            usePinchZoom: (
                                                                                                                                                            ref: RefObject<HTMLElement>
                                                                                                                                                            ) =>
                                                                                                                                                            | { zoomingState: ZoomState; pinchState: number }
                                                                                                                                                            | { zoomingState: null; pinchState: number };

                                                                                                                                                              function usePrevious

                                                                                                                                                              usePrevious: <T>(state: T) => T | undefined;

                                                                                                                                                                function usePreviousDistinct

                                                                                                                                                                usePreviousDistinct: <T>(value: T, compare?: Predicate<T>) => T | undefined;

                                                                                                                                                                  function useQueue

                                                                                                                                                                  useQueue: <T>(initialValue?: T[]) => QueueMethods<T>;

                                                                                                                                                                    function useRaf

                                                                                                                                                                    useRaf: (ms?: number, delay?: number) => number;

                                                                                                                                                                      function useRafLoop

                                                                                                                                                                      useRafLoop: (
                                                                                                                                                                      callback: FrameRequestCallback,
                                                                                                                                                                      initiallyActive?: boolean
                                                                                                                                                                      ) => RafLoopReturns;

                                                                                                                                                                        function useRafState

                                                                                                                                                                        useRafState: <S>(
                                                                                                                                                                        initialState: S | (() => S)
                                                                                                                                                                        ) => [S, Dispatch<SetStateAction<S>>];

                                                                                                                                                                          function useRendersCount

                                                                                                                                                                          useRendersCount: () => number;

                                                                                                                                                                            function useScratch

                                                                                                                                                                            useScratch: (
                                                                                                                                                                            params?: ScratchSensorParams
                                                                                                                                                                            ) => [(el: HTMLElement | null) => void, ScratchSensorState];

                                                                                                                                                                              function useScroll

                                                                                                                                                                              useScroll: (ref: RefObject<HTMLElement>) => State;

                                                                                                                                                                                function useScrollbarWidth

                                                                                                                                                                                useScrollbarWidth: () => number | undefined;

                                                                                                                                                                                  function useScrolling

                                                                                                                                                                                  useScrolling: (ref: RefObject<HTMLElement>) => boolean;

                                                                                                                                                                                    function useSessionStorage

                                                                                                                                                                                    useSessionStorage: <T>(
                                                                                                                                                                                    key: string,
                                                                                                                                                                                    initialValue?: T,
                                                                                                                                                                                    raw?: boolean | undefined
                                                                                                                                                                                    ) => [T, (value: T) => void];

                                                                                                                                                                                      function useSet

                                                                                                                                                                                      useSet: <K>(initialSet?: Set<K>) => [Set<K>, Actions<K>];

                                                                                                                                                                                        function useSetState

                                                                                                                                                                                        useSetState: <T extends object>(
                                                                                                                                                                                        initialState?: T
                                                                                                                                                                                        ) => [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void];

                                                                                                                                                                                          function useShallowCompareEffect

                                                                                                                                                                                          useShallowCompareEffect: (effect: EffectCallback, deps: DependencyList) => void;

                                                                                                                                                                                            function useSize

                                                                                                                                                                                            useSize: (
                                                                                                                                                                                            element: Element,
                                                                                                                                                                                            { width, height }?: Partial<State>
                                                                                                                                                                                            ) => [React.ReactElement<any>, State];

                                                                                                                                                                                              function useSlider

                                                                                                                                                                                              useSlider: (ref: RefObject<HTMLElement>, options?: Partial<Options>) => State;

                                                                                                                                                                                                function useSpeech

                                                                                                                                                                                                useSpeech: (text: string, options: ISpeechOptions) => ISpeechState;

                                                                                                                                                                                                  function useStartTyping

                                                                                                                                                                                                  useStartTyping: (onStartTyping: (event: KeyboardEvent) => void) => void;

                                                                                                                                                                                                    function useStateList

                                                                                                                                                                                                    useStateList: <T>(stateSet?: T[]) => UseStateListReturn<T>;

                                                                                                                                                                                                      function useStateValidator

                                                                                                                                                                                                      useStateValidator: <V extends ValidityState, S>(
                                                                                                                                                                                                      state: S,
                                                                                                                                                                                                      validator: StateValidator<V, S>,
                                                                                                                                                                                                      initialState?: V
                                                                                                                                                                                                      ) => UseStateValidatorReturn<V>;

                                                                                                                                                                                                        function useStateWithHistory

                                                                                                                                                                                                        useStateWithHistory: {
                                                                                                                                                                                                        <S, I extends S>(
                                                                                                                                                                                                        initialState: IHookStateInitAction<S>,
                                                                                                                                                                                                        capacity?: number,
                                                                                                                                                                                                        initialHistory?: I[]
                                                                                                                                                                                                        ): UseStateHistoryReturn<S>;
                                                                                                                                                                                                        <S = undefined>(): UseStateHistoryReturn<S>;
                                                                                                                                                                                                        };

                                                                                                                                                                                                          function useThrottle

                                                                                                                                                                                                          useThrottle: <T>(value: T, ms?: number) => T;

                                                                                                                                                                                                            function useThrottleFn

                                                                                                                                                                                                            useThrottleFn: <T, U extends any[]>(
                                                                                                                                                                                                            fn: (...args: U) => T,
                                                                                                                                                                                                            ms: number | undefined,
                                                                                                                                                                                                            args: U
                                                                                                                                                                                                            ) => T;

                                                                                                                                                                                                              function useTimeout

                                                                                                                                                                                                              useTimeout: (ms?: number) => UseTimeoutReturn;

                                                                                                                                                                                                                function useTimeoutFn

                                                                                                                                                                                                                useTimeoutFn: (fn: Function, ms?: number) => UseTimeoutFnReturn;

                                                                                                                                                                                                                  function useToggle

                                                                                                                                                                                                                  useToggle: (initialValue: boolean) => [boolean, (nextValue?: any) => void];

                                                                                                                                                                                                                    function useTween

                                                                                                                                                                                                                    useTween: (easingName?: string, ms?: number, delay?: number) => number;

                                                                                                                                                                                                                      function useUnmount

                                                                                                                                                                                                                      useUnmount: (fn: () => any) => void;

                                                                                                                                                                                                                        function useUnmountPromise

                                                                                                                                                                                                                        useUnmountPromise: () => Race;

                                                                                                                                                                                                                          function useUpdate

                                                                                                                                                                                                                          useUpdate: () => () => void;

                                                                                                                                                                                                                            function useUpsert

                                                                                                                                                                                                                            useUpsert: <T>(
                                                                                                                                                                                                                            predicate: (a: T, b: T) => boolean,
                                                                                                                                                                                                                            initialList?: IHookStateInitAction<T[]>
                                                                                                                                                                                                                            ) => [T[], UpsertListActions<T>];
                                                                                                                                                                                                                            • Deprecated

                                                                                                                                                                                                                              Use useList hook's upsert action instead

                                                                                                                                                                                                                            function useVideo

                                                                                                                                                                                                                            useVideo: (
                                                                                                                                                                                                                            elOrProps: any
                                                                                                                                                                                                                            ) => readonly [
                                                                                                                                                                                                                            any,
                                                                                                                                                                                                                            HTMLMediaState,
                                                                                                                                                                                                                            {
                                                                                                                                                                                                                            play: () => Promise<void> | undefined;
                                                                                                                                                                                                                            pause: () => void;
                                                                                                                                                                                                                            seek: (time: number) => void;
                                                                                                                                                                                                                            volume: (volume: number) => void;
                                                                                                                                                                                                                            mute: () => void;
                                                                                                                                                                                                                            unmute: () => void;
                                                                                                                                                                                                                            },
                                                                                                                                                                                                                            any
                                                                                                                                                                                                                            ];

                                                                                                                                                                                                                              function useWindowScroll

                                                                                                                                                                                                                              useWindowScroll: () => State;

                                                                                                                                                                                                                                function useWindowSize

                                                                                                                                                                                                                                useWindowSize: (
                                                                                                                                                                                                                                initialWidth?: number,
                                                                                                                                                                                                                                initialHeight?: number
                                                                                                                                                                                                                                ) => { width: number; height: number };

                                                                                                                                                                                                                                  Package Files (112)

                                                                                                                                                                                                                                  Dependencies (14)

                                                                                                                                                                                                                                  Dev Dependencies (58)

                                                                                                                                                                                                                                  Peer Dependencies (2)

                                                                                                                                                                                                                                  Badge

                                                                                                                                                                                                                                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                                                                                                                                                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/react-use.

                                                                                                                                                                                                                                  • Markdown
                                                                                                                                                                                                                                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/react-use)
                                                                                                                                                                                                                                  • HTML
                                                                                                                                                                                                                                    <a href="https://www.jsdocs.io/package/react-use"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>