Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more immutability helpers. #1856

Closed
Gonzih opened this issue Jul 17, 2014 · 9 comments
Closed

Add more immutability helpers. #1856

Gonzih opened this issue Jul 17, 2014 · 9 comments

Comments

@Gonzih
Copy link

Gonzih commented Jul 17, 2014

Hello, I like Idea of immutability helpers in react, but I find them incomplete. I still from time to time switch to Underscore for complex operations on data. Does it make sense to add more helpers like:

  • $map
  • $filter
  • $fold
  • set like operations (union/difference/intersection), of course they can be implemented in terms of fold.

What do you think guys? Thanks.

@syranide
Copy link
Contributor

Personally, I run with much simplified helper instead, that only supports the equivalent of $apply. That allows me to work with immutable data as if it was mutable, with the regular functions for mutating.

Something like: (immutate simply shallow clones)

var nextObj = immutate(prevObj, {
  user: function(obj) {
    obj.name = 'Test';
    obj.permissions = immutate(obj.permissions, function(arr) {
      arr.push(1);
      arr.push(2);
    });
  },
});

@Gonzih
Copy link
Author

Gonzih commented Jul 17, 2014

How immutate implementation looks like?

@syranide
Copy link
Contributor

https://gist.github.com/syranide/d75f32ebad237112d324
I haven't really vetted it yet, but it should work fine.

nextObject = immutate(prevObject, keysToMutateOrMutator);

var prev = {value: 1, permissions: { ids: [1,2,3] }};

var next = immutate(prev, function(user) {
  user.name = 'next';
  user.permissions = immutate(obj.permissions, { ids: function(ids) {
    ids.push(4, 5);
    ids.shift();
  }});
});

prev before: {"name":"previous","permissions":{"ids":[1,2,3]}}
prev after:  {"name":"previous","permissions":{"ids":[1,2,3]}}
next after:  {"name":"next","permissions":{"ids":[2,3,4,5]}}

I've also thought about getting rid of the redundancy when mutating by providing a second argument to the callback, that could look like: function(user, mutate) { mutate({permissions: {ids: ... }}); } but haven't thought that much about it just yet.

PS. As always, use at your own risk :)

@Gonzih
Copy link
Author

Gonzih commented Jul 17, 2014

Well I still would love to have more complete helpers in react. If I need rich api for immutable data operations I can of course adopt Underscore for that or integrate Mori in to react. Question then is why you need helpers in react if they are incomplete?

@sophiebits
Copy link
Collaborator

var next = {
  ...prev,
  name: 'next',
  permissions: {
    ...,
    ids: prev.permissions.ids.slice(1).concat([4, 5])
  }
};

@syranide
Copy link
Contributor

@spicyj Oooh, very interesting use of the spread operator (or whatever it is called).

@jardakotesovec
Copy link

For removing element from array I can use $splice, but how I could remove property from object using immutable helpers? Just want to remove particular key.

@chenglou
Copy link
Contributor

@jardakotesovec #2362

@Gonzih: immutable-js is a fully featured immutable collection library that provides the helpers you want and more. Since update is getting deprecated/pulled out into its own repo, we can always come back to this after that.

@petehunt can we close this now?

@gaearon
Copy link
Collaborator

gaearon commented Mar 18, 2016

As far as I know the current thinking is that React.addons.update is in maintenance mode and will not get new features. We encourage people to look at Immutable.js or alternative libraries that deal with plain objects instead.

@gaearon gaearon closed this as completed Mar 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants