Skip to content

Commit 1195510

Browse files
docs: Update example-setstate.mdx (#833)
Moved id generation for new todos out of reducers to keep them pure
1 parent 648d39b commit 1195510

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

website/docs/example-setstate.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const TodoList = () => {
140140
break
141141
case "add":
142142
draft.push({
143-
id: "todo_" + Math.random(),
143+
id: action.id,
144144
title: "A new todo",
145145
done: false
146146
})
@@ -163,7 +163,8 @@ const TodoList = () => {
163163

164164
const handleAdd = useCallback(() => {
165165
dispatch({
166-
type: "add"
166+
type: "add",
167+
id: "todo_" + Math.random()
167168
})
168169
}, [])
169170

@@ -189,7 +190,7 @@ const TodoList = () => {
189190
break;
190191
case "add":
191192
draft.push({
192-
id: "todo_" + Math.random(),
193+
id: action.id,
193194
title: "A new todo",
194195
done: false
195196
});
@@ -227,7 +228,7 @@ const todosReducer = produce((draft, action) => {
227228
break
228229
case "add":
229230
draft.push({
230-
id: "todo_" + Math.random(),
231+
id: action.id,
231232
title: "A new todo",
232233
done: false
233234
})

0 commit comments

Comments
 (0)