Function useLocalStorage

  • Custom React hook that manages state synchronized with local storage.

    This hook provides a way to store and retrieve a value in local storage, keeping the state in sync with the storage. It can handle server-side rendering (SSR) scenarios and allows for a callback when local storage changes.

    Example

    const [state, setState] = useLocalStorage(
    'localStorageKey',
    { searchQuery: '' },
    );

    setState({ ...state, searchQuery: 'test' });

    Type Parameters

    • T

    Parameters

    • name: string

      The name of the local storage key.

    • defaultValueProp: T

      The default value to use if there is no value in local storage.

    • Optional onLsChangeProp: ((value) => void)

      Callback function to be called when local storage changes.

        • (value): void
        • Parameters

          • value: T

          Returns void

    Returns [T, ((value) => void)]