A flexible React Hook for persisting state to localStorage, with an API similar to React's useState. Version 3.0.0 supports React >=16.8.1, TypeScript types, SSR, and cross-tab synchronization. It uses JSON.stringify/JSON.parse by default but allows custom serializers, parsers, and error loggers. Key differentiators: automatic cross-context sync, type inference, and the ability to remove a key by setting its value to undefined. Released under MIT, actively maintained.
npm install use-local-storageNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage with a numeric default value, incrementing, and removing the localStorage key by setting undefined.
Use `import useLocalStorage from 'use-local-storage'` instead of any other import pattern.
If you want to store the literal value undefined, use JSON.stringify(undefined) manually or custom serializer.
Provide a custom serializer/parser in the options object to handle non-standard types.
Set `syncData: false` in options to disable cross-tab sync.
Initialize with a fallback value or wrap in try-catch, but the hook already catches internally.
Ensure that 'skipLibCheck' is not true in tsconfig.json or add 'use-local-storage' to types in tsconfig.
Do not call JSON.parse on the value yourself; rely on the hook's default parser or provide a custom one that checks for null/undefined.
Always provide a valid string as the first argument (the localStorage key).