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
Provenance
- Commit
a7b5df7- Tree
164888c- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
75de7af
1 file changed+5 added−67 removed
modified:readme.md+5 added-67 removed
195 unmodified lines196 * `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)199 200-## More reducer examples201-202-Here 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.203-These examples are semantically equivalent and produce the exact same state.204-205-```javascript206-// Plain reducer207-function insertItem(array, action) {208- return [209- ...array.slice(0, action.index),210- action.item,211- ...array.slice(action.index)212- ]213-}214-215-// With immer216-function insertItem(array, action) {217- return produce(array, draft => {218- draft.splice(action.index, 0, action.item)219- })220-}221-222-// Plain reducer223-function removeItem(array, action) {224- return [225- ...array.slice(0, action.index),226- ...array.slice(action.index + 1)227- ];228-}229-230-// With immer231-function removeItem(array, action) {232- return produce(array, draft => {233- draft.splice(action.index, 1)234- })235-}236-237-// Plain reducer238-function 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- }244-245- // Otherwise, this is the one we want - return an updated value246- return {247- ...item,248- ...action.item249- };250- });251-}252-253-// With immer254-function 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-```262-200 ## Pitfalls201 265-* 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.202+1. Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.203+2. 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.204+3. 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.205+4. 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`206+5. Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.207 208 ## How does Immer work?209 210 Read the (second part of the) [introduction blog](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3).27 unmodified lines