Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES6 Map with React and/or Redux

I am looking at using the ES6 Map object to do my hashing for me, instead of creating a custom hashing function. However, it seems to me that it doesn't have much support for immutability, which is a key thing for React as well as Redux.

I am used to not using any libraries for immutability, but use just plain destructuring:

const newObj = { ...prevObj, newKey: "value" }

or map/filter with arrays.

However, ES6 Map has methods that directly update the object itself.

What I could think of was to do something like this:

var myMap = new Map()
Var myNewMap = { ...myMap.set() }
this.setState({ myMap:myNewMap })

But I am not sure if that would work.

like image 963
Vincent Rye Avatar asked Dec 19 '25 19:12

Vincent Rye


1 Answers

If you just want a type-safe key-value store, you can use the following syntax:

entities: { [id: number]: MyEntity },
...
like image 153
Jared Beach Avatar answered Dec 21 '25 10:12

Jared Beach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!