Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions website/docs/example-setstate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,28 @@ const TodoList = () => {
});
}, []);

// etc
return (
<ul>
{todos.map((todo) => (
<li key={todo.id}>
<label>
<input
type="checkbox"
checked={todo.done}
onChange={() => handleToggle(todo.id)}
/>
{todo.title}
</label>
</li>
))}
<button type="button" onClick={handleAdd}>
Add todo
</button>
</ul>
);
};
```

For the full demo see [CodeSandbox](https://codesandbox.io/s/use-immer-bvd5v?file=/src/index.js).

## useReducer + Immer

Similarly to `useState`, `useReducer` combines neatly with Immer as well, as demonstrated in this [CodeSandbox](https://codesandbox.io/s/immer-usereducer-bqpzn?file=/src/index.js:0-1018):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,28 @@ const TodoList = () => {
});
}, []);

// etc
return (
<ul>
{todos.map((todo) => (
<li key={todo.id}>
<label>
<input
type="checkbox"
checked={todo.done}
onChange={() => handleToggle(todo.id)}
/>
{todo.title}
</label>
</li>
))}
<button type="button" onClick={handleAdd}>
Add todo
</button>
</ul>
);
};
```

完整的 demo 请参阅 [CodeSandbox](https://codesandbox.io/s/use-immer-bvd5v?file=/src/index.js)

## useReducer + Immer

与 `useState` 类似,`useReducer` 也与 Immer 巧妙结合,如 [CodeSandbox](https://codesandbox.io/s/immer-usereducer-bqpzn?file=/src/index.js:0-1018) 所示:
Expand Down