Took improvements from #426 and #115, added patterns page to the docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 35b80db · Browse files · 4 files·+122 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+122 added−4 removed
- other:+122 added−4 removed
Provenance
- Commit
edcc658- Tree
b9b91da- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
35b80db
4 files changed+122 added−4 removed
modified:docs/example-reducer.md+3 added-3 removed
33 unmodified lines343536373838394041426 unmodified lines495051525353545556575733 unmodified lines }}```
After using Immer, that simply becomes:After using Immer, our reducer can be expressed as:
```javascriptimport produce from "immer"
6 unmodified lines }})```
Notice that it is not needed to handle the default case, a producer that doesn't do anything will simply return the original state.Notice that it is not necessary to handle the default case, a producer that doesn't do anything will return the original state.
Creating Redux reducer is just a sample application of the Immer package. Immer is not just designed to simplify Redux reducers. It can be used in any context where you have an immutable data tree that you want to clone and modify (with structural sharing).
_Note: it might be tempting after using producers for a while, to just place `produce` in your root reducer and then pass the draft to each reducer and work directly over such draft. Don't do that. It kills the point of Redux where each reducer is testable as pure reducer. Immer is best used when applying it to small individual pieces of logic.__Note: it might be tempting after using producers for a while, to just place `produce` in your root reducer and then pass the draft to each reducer and work directly over said draft. Don't do that. It removes the benefit of using Redux as a system where each reducer is testable as a pure function. Immer is best used when applied to small, individual pieces of logic._added:docs/update-patterns.md+115 added
1+---2+id: update-patterns3+title: Update patterns4+---5+6+<div id="codefund"><!-- fallback content --></div>7+8+Working with immutable data, before Immer, used to mean learning all the immutable update patterns.9+10+To help 'unlearning' those patterns here is an overview how you can leverage the built-in JavaScript APIs to update objects and collections:11+12+```javascript13+import produce from "immer"14+15+// object mutations16+const todosObj = {17+ id1: {done: false, body: "Take out the trash"},18+ id2: {done: false, body: "Check Email"}19+}20+21+// add22+const addedTodosObj = produce(todosObj, draft => {23+ draft["id3"] = {done: false, body: "Buy bananas"}24+})25+26+// delete27+const deletedTodosObj = produce(todosObj, draft => {28+ delete draft["id1"]29+})30+31+// update32+const updatedTodosObj = produce(todosObj, draft => {33+ draft["id1"].done = true34+})35+36+// array mutations37+const todosArray = [38+ {id: "id1", done: false, body: "Take out the trash"},39+ {id: "id2", done: false, body: "Check Email"}40+]41+42+// add43+const addedTodosArray = produce(todosArray, draft => {44+ draft.push({id: "id3", done: false, body: "Buy bananas"})45+})46+47+// delete by index48+const deletedTodosArray = produce(todosArray, draft => {49+ draft.splice(3 /*the index */, 1)50+})51+52+// update by index53+const updatedTodosArray = produce(todosArray, draft => {54+ draft[3].done = true55+})56+57+// insert at index58+const updatedTodosArray = produce(todosArray, draft => {59+ draft.splice(3, 0, {id: "id3", done: false, body: "Buy bananas"})60+})61+62+// remove last item63+const updatedTodosArray = produce(todosArray, draft => {64+ draft.pop()65+})66+67+// remove first item68+const updatedTodosArray = produce(todosArray, draft => {69+ draft.shift()70+})71+72+// add item at the beginning of the array73+const addedTodosArray = produce(todosArray, draft => {74+ draft.unshift({id: "id3", done: false, body: "Buy bananas"})75+})76+77+// delete by id78+const deletedTodosArray = produce(todosArray, draft => {79+ const index = draft.findIndex(todo => todo.id === "id1")80+ if (index !== -1) draft.splice(index, 1)81+})82+83+// update by id84+const updatedTodosArray = produce(todosArray, draft => {85+ const index = draft.findIndex(todo => todo.id === "id1")86+ if (index !== -1) draft[index].done = true87+})88+89+// example complex data structure90+const store = {91+ users: new Map([92+ [93+ "17",94+ {95+ name: "Michel",96+ todos: [97+ {98+ title: "Get coffee",99+ done: false100+ }101+ ]102+ }103+ ]104+ ])105+}106+107+// updating something deeply in-an-object-in-an-array-in-a-map-in-an-object:108+const nextStore = produce(store, draft => {109+ draft.users.get("17").todos[0].done = true110+})111+```112+113+Note that many array operations can be used to insert multiple items at once by passing multiple arguments or using the spread operation: `todos.unshift(...items)`.114+115+Note that when working with arrays that contain objects that are typically identified by some id, we recommend to use `Map` or index based objects (as shown above) instead of performing frequent find operations, lookup tables perform much better in general.modified:website/i18n/en.json+3 added
74 unmodified lines757677787980818283848515 unmodified lines74 unmodified lines }, "typescript": { "title": "Using TypeScript or Flow", "sidebar_label": "TypeScript / Flow" }, "update-patterns": { "title": "Update patterns" } }, "links": { "Documentation": "Documentation",15 unmodified linesmodified:website/sidebars.json+1 added-1 removed
12334567{ "Immer": { "Basics": ["introduction", "installation", "produce", "curried-produce", "example-reducer", "example-setstate"], "Basics": ["introduction", "installation", "produce", "curried-produce", "example-reducer", "example-setstate", "update-patterns"], "Advanced Features": ["api", "map-set", "complex-objects", "current", "original", "patches", "freezing", "return", "async", "typescript"], "Resources": ["performance", "resources", "faq", "pitfalls", "built-with", "support"] }}