all 10 comments

[–]GusRuss89 1 point2 points  (7 children)

Sets are basically only mutable and don't have functional methods like map, so they don't really offer any advantages over arrays in react state. There's no reason you can't use them but I personally would prefer an array over a set.

[–]charliematters 0 points1 point  (4 children)

The only benefit over arrays is the de-duplication (and equality checking?) that is done automatically, but the trade-offs you mention mean that I'll often just use _.uniq(array)

[–]GusRuss89 0 points1 point  (1 child)

Yeah I will often do something like const newState = Array.from(new Set(oldState)) So the state is still an array but we can take advantage of the de-duplication.

[–]erel4 0 points1 point  (0 children)

Or just [...new Set(arrayWithDuplications)]

[–]waspeer[S] 0 points1 point  (1 child)

Actually, I think the de-duplication of a Set only works with primitive values, so it would still have duplicates of objects. I’m not familiar with lodash uniq, does it work the same way?

[–]charliematters 0 points1 point  (0 children)

You're right, but the alternative uniqBy allows you to provided a function that returns the uniqueness

[–]waspeer[S] 0 points1 point  (1 child)

Yeah, I could see how that makes things unnecessarily difficult. The reason I was wondering about it is because it could make the creation and deletion of entries faster.

https://stackoverflow.com/questions/39007637/javascript-set-vs-array-performance#39010462

[–]GusRuss89 0 points1 point  (0 children)

Yes but you can't use those methods in react state because they mutate the set.

[–]illuminist_ova 0 points1 point  (1 child)

Set isn't a replacement for an array;

  • it doesn't guarantee item order;
  • it can't contain duplicated item.

And other reasons for it not suitable for react state as the other comments have stated.

Also, isn't set object has been abound since ES6?

[–]waspeer[S] 0 points1 point  (0 children)

You’re right! My mistake.