Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+5 added−3 removed
other:+5 added−3 removed
Provenance
Commit
c674cad
Tree
0bc439f
Author
Michel Weststrate <mweststrate@gmail.com>
Committer
Michel Weststrate <mweststrate@gmail.com>
Parent
81d9d78
1 file changed+5 added−3 removed
modified:readme.md+5 added-3 removed
119 unmodified lines
Expand all
120
121
122
123
124
124
125
126
127
127
128
129
130
131
132
133
134
134
135
136
137
138
139
9 unmodified lines
149
150
151
152
153
119 unmodified lines
Expand all
```javascript
import immer from 'immer'
const todos = (state = [], action) =>
// immer produces nextState from draftState and returns it
immer(state,state=> {
immer(state,draftState=> {
switch (action.type) {
case 'ADD_TODO':
state.push({
draftState.push({
id: action.id,
text: action.text,
completed: false
})
return
case 'TOGGLE_TODO':
const todo =state.find(todo => todo.id === action.id)
const todo =draftState.find(todo => todo.id === action.id)
todo.completed = !todo.completed
return
}
})
9 unmodified lines
## Pitfalls:
* Make sure to modify the state you get passed in in the callback function, not the original base state that was passed as first argument to `immer`!
* Since immer uses proxies, reading huge amounts of data from state comes with an overhead. If this ever becomes an issue (measure before optimize!), do the current state analysis before entering the `immer` block or read form the `currentState` rather than the `draftState`