Docs

Store Utilities API

Access and manipulate your global state outside of React components. Perfect for syncing state with storage, API headers, or performance optimization.

#initState(key, initialValue)

Initializes a global key at module scope, outside React components. Useful for service files, auth modules, and logging middleware.

ts
app/page.tsx
import { initState } from "react-use-superstate"
// Initialize at module scope (not inside a component)
initState('authUser', null)
initState('theme', 'light')

#getState(key)

Synchronously retrieves the current value of a global key.

ts
app/page.tsx
import { getState } from "react-use-superstate"
const user = getState('user')
console.log(user.name) // "Ahsan"

#setState(key, value | functional)

Updates the state of a global key from anywhere. Accepts a direct value or a functional update.

ts
app/page.tsx
import { setState } from "react-use-superstate"
// Direct update
setState('count', 10)
// Functional update (preferred for complex logic)
setState('user', (prev) => ({ ...prev, age: prev.age + 1 }))

#batchUpdate(callback)

Bundles multiple updates into a single notification. Prevents multiple re-renders of the same component.

ts
app/page.tsx
import { batchUpdate, setState } from "react-use-superstate"
batchUpdate(() => {
setState('count', 1)
setState('theme', 'dark')
setState('isOpen', true)
})
// Result: One atomic re-render for affected components.

#resetState(key?)

Resets a specific key (or the entire store if no key provided) to its original initial value. Triggers notifications.

ts
app/page.tsx
import { resetState } from "react-use-superstate"
// Reset specific key
resetState('count')
// Reset EVERYTHING to initial values
resetState()

#destroyStore()

Completely clears the global registry and notifies all active listeners. Useful for testing or entire app teardown.

ts
app/page.tsx
import { destroyStore } from "react-use-superstate"
// Notify components and wipe the registry
destroyStore()
Memory Caution
Always reset state using resetState(key) or perform a complete teardown with destroyStore() when cleaning up large data structures to maintain peak performance.