Easily persist your states across sessions with minimal effort. react-persisted-state
extends the familiar useState
hook to allow state persistence in localStorage
, sessionStorage
, cookies
, or even through custom adapters. This library is designed to be fully flexible, allowing you to maintain state persistence with identical usability to React's own state hook, including inferred typing, state setting functions, initial state definitions, and more.
- Seamless State Persistence: Automatically persists state across page reloads and sessions.
- Plug-and-Play: Uses
localStorage
by default but easily switches tosessionStorage
,cookies
or other custom adapters. - Custom Storage Adapters: Extend persistence to any storage by implementing
IPersistedAdapter
. - Fully Typed: TypeScript support out-of-the-box, with inferred types for your state.
- Minimal API: Designed to be a drop-in replacement for React's
useState
.
Using npm
npm install react-persisted-state
Using yarn
yarn add react-persisted-state
Using pnpm
pnpm add react-persisted-state
Here are various ways to use react-persisted-state
, demonstrating its flexibility:
import { usePersistedState } from 'react-persisted-state';
const [count, setCount] = usePersistedState('countKey', 0);
import { usePersistedState } from 'react-persisted-state';
const [userSession] = usePersistedState<string | null>('userSessionKey', null, 'sessionStorage');
/**
* Or you can use directly `window.sessionStorage`
*/
const [userSession] = usePersistedState<string | null>('userSessionKey', null, window.sessionStorage);
import { usePersistedState, CookiePersistedAdapter } from 'react-persisted-state';
const [theme, setTheme] = usePersistedState<Theme>('theme', 'light', 'cookies');
/**
* Or you can a direct instance of CookiePersistedAdapter
*/
const [theme, setTheme] = usePersistedState<Theme>('theme', 'light', new CookiePersistedAdapter());
// Create your implementation
import { usePersistedState, IPersistedAdapter } from 'react-persisted-state';
class CustomPersistedAdapter implements IPersistedAdapter {
getItem(key: string): string {
// fetch your data and return in JSON format
}
setItem(key: string, value: string): void {
// store your data in JSON format
}
removeItem(key: string): void {
// remove your data
}
}
// And use it
const [state, setState] = usePersistedState('stateKey', null, new CustomPersistedAdapter());
import { usePersistedState } from 'react-persisted-state';
const [theme, setTheme, removePersistedTheme] = usePersistedState<Theme>('theme', 'light');
/**
* This will remove the persisted theme from localStorage
*/
removePersistedTheme();
Implement IPersistedAdapter
to customize storage methods, allowing you to use any storage mechanism your prefer. The adapter requires three methods: getItem
, setItem
and removeItem
. Look at the CustomPersistedAdapter
above example to see how to implement it.
Contributions are welcome! Whether it's feature requests, bug reports, or pull requests, feel free to contribute. For more details, check out CONTRIBUTING.md.