Skip to content

nexa-state

Global and local state management built on top of Nexa signals.

createStore

Creates a reactive store from a plain object. Each property becomes a Signal.

ts
import { createStore } from 'nexa-state'

function createStore<T extends Record<string, any>>(
  initialState: T,
  persistOptions?: PersistOptions
): Store<T>

Store<T>

ts
type Store<T> = {
  [K in keyof T]: Signal<T[K]>
}

Each key of the initial object becomes a signal with the same name.

Examples:

ts
const store = createStore({ count: 0, text: 'hello' })
store.count.value = 5
store.text.value = 'world'

With persistence:

ts
const store = createStore({ theme: 'light' }, persist('app-settings'))

defineStore

Defines a singleton store that is lazily instantiated on first use.

ts
import { defineStore } from 'nexa-state'

function defineStore<T>(
  id: string,
  factory: () => T,
  persistOptions?: PersistOptions
): () => T

Returns a hook function that returns the singleton instance.

Examples:

ts
export const useCounter = defineStore('counter', () => {
  const count = 0
  const increment = () => useCounter().count.value++
  return { count, increment }
})

// In a component:
const counter = useCounter()
counter.increment()

With persistence:

ts
export const useSettings = defineStore(
  'settings',
  () => ({ theme: 'light', lang: 'en' }),
  persist('user-settings')
)

persist

A persist configuration factory for createStore and defineStore.

ts
import { persist } from 'nexa-state'

function persist(key: string): PersistOptions
ParameterDescription
keylocalStorage key used to serialize and restore state

The store automatically saves to localStorage on every write and reads the saved value on initialization.

Released under the MIT License.