Skip to content
Myr

Removed superfluos Redux examples (fixes #57). Explained working with Dates better

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 75de7af · Browse files · 1 file+5 added−67 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+5 added−67 removed

  • other:+5 added−67 removed

1 file+5 added−67 removed

modified:readme.md+5 added-67 removed
195 unmodified lines
196* `import produce from "immer"`, the default, ships with both the Proxy and ES5 compatible implementation, and picks at runtime the best matching one. This is the most convenient, but adds the most to the bundle size (~2KB)197* `import produce from "immer/proxy"`: This build is optimized for modern browser, which support Proxies and other modern language features, and doesn't polyfill things like `Symbol`. Use this if you are targetting a modern environment only198* `import produce from "immer/es5"`: Only bundle the ES5 compatible implementation (which of course also works for modern browsers)199200## More reducer examples201202Here are some typical reducer examples, take from the Redux [Immutable Update Patterns](https://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html) page, and their Immer counter part.203These examples are semantically equivalent and produce the exact same state.204205```javascript206// Plain reducer207function insertItem(array, action) {208 return [209 ...array.slice(0, action.index),210 action.item,211 ...array.slice(action.index)212 ]213}214215// With immer216function insertItem(array, action) {217 return produce(array, draft => {218 draft.splice(action.index, 0, action.item)219 })220}221222// Plain reducer223function removeItem(array, action) {224 return [225 ...array.slice(0, action.index),226 ...array.slice(action.index + 1)227 ];228}229230// With immer231function removeItem(array, action) {232 return produce(array, draft => {233 draft.splice(action.index, 1)234 })235}236237// Plain reducer238function updateObjectInArray(array, action) {239 return array.map( (item, index) => {240 if(index !== action.index) {241 // This isn't the item we care about - keep it as-is242 return item;243 }244245 // Otherwise, this is the one we want - return an updated value246 return {247 ...item,248 ...action.item249 };250 });251}252253// With immer254function updateObjectInArray(array, action) {255 return produce(array, draft => {256 draft[action.index] = { ...item, ...action.item}257 // Alternatively, since arbitrarily deep updates are supported:258 // Object.assign(draft[action.index], action.item)259 })260}261```262200## Pitfalls201265* Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.266* Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` or whatever complex object from the draft state, it will be that very same object in both the base state as the next state. In such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.267* Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`268* Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.2021. Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.2032. Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` (or whatever complex object from the draft state), the changes will be persisted. But, both in your new and old state! So, in such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.2043. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.2054. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`2065. Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.207208## How does Immer work?209210Read the (second part of the) [introduction blog](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3).
27 unmodified lines