Interface LocalStorageOptions

interface LocalStorageOptions {
    SSR?: boolean;
    initialValue?: string;
    listenForChanges?: boolean;
    propagateChanges?: boolean;
}

Properties

SSR?: boolean

This parameter which is true by default determines if the hook should take the necessary steps to avoid hydration errors when using SSR. This results in the initial state being null on the first render even if there is a value in local storage. On consecutive renders the correct value will be returned. Note that components with "use client" in Next.js are still pre-rendered on the server. Only set this variable to false, if you are rendering on the client only. What this will do, it will return the correct value from local storage on the first render. This can lead to hydration errors when using SSR because local storage doesn't exist on the server.

initialValue?: string

If there is no value for the specified key, it automatically set this value on mount.

listenForChanges?: boolean

If this parameter is true, the hook listens for changes of the specified key and updates its state accordingly.

propagateChanges?: boolean

If this is set to true, it sends out a custom event to notify other instances of this hook of changes for the specified key and allows them to update accordingly. This is not intended for global state management. For global state management, please use a library like Zustand.