Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "use-localstorage"

Index

Functions

tryParse

  • tryParse(value: string): any
  • This exists for trying to serialize the value back to JSON. If it cannot serialize it, then it was a string value given.

    Parameters

    • value: string

      the value you wish to try to parse

    Returns any

useLocalStorage

  • useLocalStorage<TValue>(key: string): [TValue | null, (newValue: TValue | null) => void, () => void]
  • useLocalStorage<TValue>(key: string, defaultValue: TValue): [TValue, (newValue: TValue | null) => void, () => void]
  • React hook to enable updates to state via localStorage. This updates when the {writeStorage} function is used, when the returned function is called, or when the "storage" event is fired from another tab in the browser. This function takes an optional default value to start off with.

    example
    const MyComponent = () => {
      const [myStoredItem, setMyStoredItem] = useLocalStorage('myStoredItem');
      return (
        <p>{myStoredItem}</p>
      );
    };
    export

    Type parameters

    • TValue

      The type of the given default value.

    Parameters

    • key: string

      The key in the localStorage that you wish to watch.

    Returns [TValue | null, (newValue: TValue | null) => void, () => void]

    An array containing the value associated with the key in position 0, a function to set the value in position 1, and a function to delete the value from localStorage in position 2.

  • Type parameters

    • TValue

    Parameters

    • key: string
    • defaultValue: TValue

    Returns [TValue, (newValue: TValue | null) => void, () => void]

Generated using TypeDoc