question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to mutate immutables?

See original GitHub issue

ImmutableJS sucked becauase of its overly verbose way to access data. But it had nice mutators.

If I’m using it properly, seamless-immutable relies on what methods return a new value, so we’re missing a lot of how to mutate data. Is there a cleaner way to push a new element onto an array? Currently I’m doing something like this:

    let updatedArray = this.state.arr.asMutable();
    updatedArray.push(newElement);
    this.setState({arr: new Immutable(updatedArray)});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tad-lispycommented, Feb 26, 2016

That’s true, that delete would be useful, however the name doesn’t seem safe - it can easily collide with reserved keyword. I would actually call it without.

In one of my projects I came up with this helper function:

remove_item = function(index, array) {
    return array.slice(0, index).concat(array.slice(index + 1));
};

Then you can remove 3rd item by calling:

arr = remove_item(2, arr)

I can make a PR to use it as a method of immutable array. What do you think, @rtfeldman ?

As for the push - is there anything wrong with concat?

0reactions
slashwhatevercommented, Oct 19, 2016

Did this ever get implemented or discussed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Immutability in React: Should you mutate objects?
Over 200k developers use LogRocket to create better digital experiences ... The most simple solution is to use immutable objects. If the object ......
Read more >
Reading 9: Mutability & Immutability
Since String is immutable, once created, a String object always has the same value. ... Notice that this method does its job by...
Read more >
How to Mutate Data in a System Designed for Immutable ...
In our Field Guide series, we share challenges and successes we've encountered while exploring uncharted territory.
Read more >
Convenient Way to Mutate Immutable Objects
method to clone a new immutable object with a new property value? The mutate function above demonstrated how to mutate an immutable object ......
Read more >
Immutable Update Patterns
Structuring Reducers > Immutable Update Patterns: How to correctly ... These lead to accidental direct mutation, and should be avoided.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found