122 components

useLocalStorage@djangocfg/ui-nextjs

Persist state to localStorage with automatic serialization

useLocalStorage Hook
Persist state to localStorage with automatic serialization

Simple String Value:

localStorage['user-name'] = ""

Complex Object Value:

Theme: light
Notifications: On
Auto-save: Off
localStorage['user-settings'] = { "theme": "light", "notifications": true, "autoSave": false }

Persistence: These values are stored in localStorage and will persist even after you refresh the page or close the browser. Try refreshing to see!

Usage:

const [settings, setSettings] = useLocalStorage('user-settings', {
  theme: 'dark',
  notifications: true
});

// Updates both state and localStorage
setSettings({ ...settings, theme: 'light' });

Import

Loading code...