Docs
Store
With Immer

With Immer

import { produce } from 'immer';
 
const useTodoStore = createStore(({ set }) => ({
  todos: [
    { title: 'Learn JavaScript', done: true },
    { title: 'Try Immer', done: false },
  ],
 
  // Without immer
  toggleTodoWithoutImmer: (index) => {
    set(({ todos }) => {
      const nextTodos = todos.slice(); // Shallow clone the array
      nextTodos[index] = { ...nextTodos[index], done: !nextTodos[index].done };
      return { todos: nextTodos };
    });
  },
 
  // With immer
  toggleTodo: (index) => {
    set(
      produce(({ todos }) => {
        todos[index].done = !todos[index].done;
      })
    );
  },
}));

View Immer docs: https://immerjs.github.io/immer/ (opens in a new tab)