This skill helps you manage grouped React state and localStorage persistence with ahooks, improving form, settings, and UI consistency.
npx playbooks add skill blockmatic-icebox/basilic-old --skill ahooks-v3
useSetState for grouped state that changes together (not URL-shareable)useLocalStorageState for persistent state across browser sessionsuseState only for simple independent state (rare)useSetState for grouped state not in URL (form state, game engine state, ephemeral UI state)useLocalStorageState for localStorage persistenceuseLocalStorageState<boolean>('key', { defaultValue: false })Use for grouped state that updates together:
import { useSetState } from 'ahooks'
const [state, setState] = useSetState({
name: '',
email: '',
age: 0,
})
// Partial updates (shallow merge)
setState({ name: 'John' })
setState(prev => ({ ...prev, email: 'john@example.com' }))
When to use: Form state, game engine state, UI state that changes together (if not URL-shareable)
When NOT to use: URL-shareable state (use nuqs), loading states (use TanStack Query)
Use for state that persists across browser sessions:
import { useLocalStorageState } from 'ahooks'
const [value, setValue] = useLocalStorageState<boolean>('key', {
defaultValue: false,
})
// Type-safe with generics
const [settings, setSettings] = useLocalStorageState<Settings>('settings', {
defaultValue: { theme: 'light', fontSize: 14 },
})
When to use: User preferences, debug flags, settings that should persist across sessions
When NOT to use: URL-shareable state (use nuqs), sensitive data (use secure storage)
Bidirectional sync between URL and localStorage:
import { useLocalStorageState } from 'ahooks'
import { useQueryState } from 'nuqs'
import { useEffect, useRef } from 'react'
const [queryState, setQueryState] = useQueryState('debug')
const [storageState, setStorageState] = useLocalStorageState<boolean>('debug', {
defaultValue: false,
})
const isFirstMount = useRef(true)
// Sync on mount: localStorage → URL
useEffect(() => {
if (isFirstMount.current) {
isFirstMount.current = false
if (storageState && queryState !== 'true') {
setQueryState('true')
return
}
}
// Sync changes: URL → localStorage
if (queryState === 'true' && !storageState) {
setStorageState(true)
} else if (queryState === 'false' && storageState) {
setStorageState(false)
}
}, [queryState, storageState, setQueryState, setStorageState])
Use case: Debug flags, feature toggles that should be both URL-shareable and persistent
nuqs (filters, search, tabs, pagination)useSetState (form state, game engine, ephemeral UI)useLocalStorageState (preferences, settings)useState (rare, prefer other options)Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.