Immer
https://gyazo.com/9a0cc0a01c2d0adb5e56c6789c95418e
code:before.js
// Shallow Clone
const nextState = baseState.slice();
// Include another state value for update
done: true,
};
// Updating by push.
// In this way, you will violate the immutability principles
nextState.push({ title: 'Tweet about it' });
code:after.js
import produce from 'immer';
const nextState = produce(baseState, draft => {
draft.push({ title: 'Tweet about it' });
});
code:curried.js
const toggleTodo = (state, id) => {
return produce(state, draft => {
const todo = draft.find(t => t.id === id);
todo.done = !todo.done;
});
};
// 同等の処理が以下の書き方でできる
const toggleTodo = produce((draft, id) => {
const todo = draft.find(t => t.id === id);
todo.done = !todo.done;
});