-
-
Notifications
You must be signed in to change notification settings - Fork 859
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Include common add, delete, and update patterns in README? #115
Comments
Note that |
Added, closing |
In your examples, the delete of an element from array will fail if |
@mweststrate The examples are gone now. Please consider adding them to the documentation. The docs are very lacking in their current state. |
One thing I'm not sure of: do deletes in nested arrays work within I keep getting this error:
Given a state like this: {
positions: [ [1] ]
} I want to delete {
positions: []
} I've tried several variations of deleting objects from an array but I keep getting the same error:
Example code: const deletePosition = produce((schema, {id}) => {
for (const row of schema.positions) {
if (isArray(row)) {
let index = row.indexOf(id);
if (index > -1) {
row.splice(index, 1);
}
if (isEmpty(row)) {
schema.positions.splice(schema.positions.indexOf(row), 1);
}
}
}
}) I also tried:
But, it still results in the same error. |
Amazing |
Is there any best practice for updating sub-items?
|
yes, if you need a spread in immer, you are probably missing the point :)
|
sorry, that came out wrong :'). What I wanted to point at, if you need a spread, that is generally a good flag that it can probably expressed in a simpler way when using immer. |
Pleaaaasee add this 👍 👍 👍 |
* Introduced `current`, which takes a snapshot of the current state of a draft and finalizes it (but without freezing). Current is a great utility to print the current state during debugging (no Proxies in the way), and the output of current can also be safely leaked outside the producer. Implements #441, #591 * [BREAKING CHANGE] getters and setters are now handled consistently: own getters and setters will always by copied into fields (like Object.assign does), inherited getters and setters will be left as-is. This should allow using Immer directly on objects that trap their fields, like down in Vue or MobX. Fixes #584, #439, #593, #558 * [BREAKING CHANGE] produce no longer accepts non-draftable objects as first argument * [BREAKING CHANGE] original can only be called on drafts and will throw otherwise (fixes #605) * [BREAKING CHANGE] non-enumerable and symbolic fields will never be frozen * [BREAKING CHANGE] the patches for arrays are now computed differently to fix some scenarios in which they were incorrect. In some cases they will be more optimal now, in other cases less. Especially splicing / unshifting items into an existing array might result in a lot of patches. Fixes #468 * Improved documentation in several areas, there is now a page for typical update patterns and a separate page on how to work with classes. And additional performance tips have been included. Fixes #457, #115, #462 * Fixed #462: All branches of the produced state should be frozen * Fixed #588: Inconsistent behavior with nested produce * Fixed #577: Immer might not work with polyfilled symbols * Fixed #514, #609: Explicitly calling `useProxies(false)` shouldn’t check for the presence of Proxy.
Sorry for ignoring this issue so long, it always bungled at the end of my to-do list. There is now a dedicated page again for mutable updates! https://immerjs.github.io/immer/docs/update-patterns |
|
Both are valid
…On Thu, 25 Jun 2020, 19:02 Chirantha Dulanjala, ***@***.***> wrote:
import produce, {original} from "immer"; or import {original, produce} from "immer" // docs shows both, ???
// delete (if order of array doesn't matter)
const deletedTodosArray = produce(todosArray, draft => {
const data= original(draft); // for large data sets
const index = data.findIndex(todo => todo.id === "id1");
draft[index] = draft[data.length - 1]; // much faster than splice
draft.pop();
});
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#115 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBBXKDINXUJLRERMIBTRYOGMRANCNFSM4ETFQP7Q>
.
|
does .map should work? |
Please refrain from comment on closed issues. |
I've spent so long thinking about immutable ways to update objects and arrays that I had trouble wrapping my head around using mutating methods again, as is needed with
immer
.It would've helped me get started faster if simple "best practice" examples similar to these were included in the docs:
...though I'm not positive
draft.splice(draft.findIndex(todo => todo.id === "id1"), 1)
is the best way to delete an item from an array mutably. This is a case where the immutabledraft.filter(todo => todo.id !== "id1")
seems less verbose.The text was updated successfully, but these errors were encountered: