@illuxiza/nanostores-immer

Immer integration for nanostores, providing immutable state updates with mutable syntax
9,732 11
Install
npm install @illuxiza/nanostores-immer
Latest Version:0.1.0
License:MIT
Last Updated:Apr 25, 2025
Links: GitHub  ·  npm
Maintainer: illuxiza

Nanostores Immer

Immer integration for nanostores, providing immutable state updates with mutable syntax.

  • Small. Zero dependencies except for nanostores and immer.
  • Simple. Just use mut() method to update state with Immer.
  • Type Safe. Written in TypeScript with good type inference.
import { atom } from '@illuxiza/nanostores-immer'

// Create a store
const $users = atom({ 
  admins: [
    { id: 1, name: 'John' }
  ],
  users: [
    { id: 2, name: 'Jane' }
  ]
})

// Update with Immer's mutable syntax
$users.mut(draft => {
  draft.admins.push({ id: 3, name: 'Bob' })
  draft.users[0].name = 'Jane Doe'
})

Install

npm install @illuxiza/nanostores-immer

Usage

Basic Usage

Import the atom from this package instead of nanostores:

import { atom } from '@illuxiza/nanostores-immer'

const $store = atom({ count: 0 })

// Update state with Immer
$store.mut(draft => {
  draft.count++
})

// Regular updates still work
$store.set({ count: 2 })

With React

import { useStore } from '@nanostores/react'
import { atom } from '@illuxiza/nanostores-immer'

const $todos = atom([
  { id: 1, text: 'Buy milk', done: false }
])

export const TodoList = () => {
  const todos = useStore($todos)
  
  const toggleTodo = (id: number) => {
    $todos.mut(draft => {
      const todo = draft.find(t => t.id === id)
      if (todo) todo.done = !todo.done
    })
  }

  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id} onClick={() => toggleTodo(todo.id)}>
          {todo.text} {todo.done ? '✓' : ''}
        </li>
      ))}
    </ul>
  )
}

TypeScript Support

The package is written in TypeScript and provides good type inference:

interface User {
  id: number
  name: string
  settings: {
    theme: 'light' | 'dark'
    notifications: boolean
  }
}

const $user = atom<User>({
  id: 1,
  name: 'John',
  settings: {
    theme: 'light',
    notifications: true
  }
})

// TypeScript will ensure type safety
$user.mut(draft => {
  draft.settings.theme = 'dark' // ✓ OK
  draft.settings.theme = 'blue' // ✗ Type Error
})

License

MIT

Credits

  • Nanostores - The original state manager
  • Immer - Immutable state with a mutable API

Related Packages

nanostores

A tiny (351 bytes) state manager for React/Preact/Vue/Svelte with many atomic tr...

202,069,944 7,630
nanostores-cjs

A tiny (266 bytes) state manager for React/Preact/Vue/Svelte with many atomic tr...

11,648 7,630
@nanostores/deepmap

A helper for Nanostores to create deep maps

220,586 9
@logux/state

A tiny (152 bytes) state manager for React/Preact/Vue/Svelte with many atomic tr...

35,227 7,630
@depup/nanostores

A tiny (351 bytes) state manager for React/Preact/Vue/Svelte with many atomic tr...

1,302 7,630

Version History

Version Released License
0.1.0 MIT