Comparing version 0.2.0 to 0.2.1
# Changelog | ||
### 0.2.1 | ||
* Fixed: `immer/es5.js` was not packed into the package. PR [#28](https://github.com/mweststrate/immer/pull/28) by [Nicolas Lepage](https://github.com/nlepage) | ||
### 0.2.0 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "immer", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Create your next immutable state by mutating the current one", | ||
@@ -32,2 +32,3 @@ "main": "immer.js", | ||
"immer.js", | ||
"es5.js", | ||
"index.d.ts" | ||
@@ -34,0 +35,0 @@ ], |
@@ -173,2 +173,65 @@ # Immer | ||
--- | ||
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. | ||
These examples are semantically equivalent and produce the exact same state. | ||
```javascript | ||
// Plain reducer | ||
function insertItem(array, action) { | ||
return [ | ||
...array.slice(0, action.index), | ||
action.item, | ||
...array.slice(action.index) | ||
] | ||
} | ||
// With immer | ||
function insertItem(array, action) { | ||
return immer(array, draft => { | ||
draft.splice(action.index, 0, action.item) | ||
}) | ||
} | ||
// Plain reducer | ||
function removeItem(array, action) { | ||
return [ | ||
...array.slice(0, action.index), | ||
...array.slice(action.index + 1) | ||
]; | ||
} | ||
// With immer | ||
function removeItem(array, action) { | ||
return immer(array, draft => { | ||
draft.splice(action.index, 1) | ||
}) | ||
} | ||
// Plain reducer | ||
function updateObjectInArray(array, action) { | ||
return array.map( (item, index) => { | ||
if(index !== action.index) { | ||
// This isn't the item we care about - keep it as-is | ||
return item; | ||
} | ||
// Otherwise, this is the one we want - return an updated value | ||
return { | ||
...item, | ||
...action.item | ||
}; | ||
}); | ||
} | ||
// With immer | ||
function updateObjectInArray(array, action) { | ||
return immer(array, draft => { | ||
draft[action.index] = { ...item, ...action.item} | ||
// Alternatively, since arbitrarily deep updates are supported: | ||
// Object.assign(draft[action.index], action.item) | ||
}) | ||
} | ||
``` | ||
## Performance | ||
@@ -175,0 +238,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30772
7
461
273