0

I'm new to Immutable.js, so this is a very trivial question.

It looks like I can't get a Map value like with plain old Javascript objects, e.g. myMap.myKey. Apparently I have to write myMap.get('myKey')

I am very surprised by this behavior. Is there a reason for that? Is there any extension to Immutable.js which would allow me to type myMap.myKey?

vcarel
  • 1,787
  • 1
  • 16
  • 23
  • Use a `Record`. But beware, it doesn't like deeply nested structures. Immutable does this to ensure structural sharing. – hazardous May 03 '16 at 16:30

2 Answers2

1

Came back to elaborate on my comment, but SO doesn't allow that after certain time. Converting it into an answer.

The question you have asked has been reciprocated several times with people who start new with immutable, yours truly included. Its on one of the rants I wrote a while ago.

It starts to make sense when you look at it from immutability perspective. If you expose value types as your own properties, they won't be immutable because they are value types and could be assigned to.

Nonetheless, its frustrating to spread these getters all across your components/views. If you can afford it, you should try to use the Record type. It offers traditional access to members (except in IE 8). Better still, you can extend from this type and add helper getters/setters (e.g. user.getName(), user.setName('thebat') instead of user.get('name')/set('name', 'thebat')) to abstract your model's internal structure from your views. However there are challenges to overcome like nested structures and de-serialization of objects.

If the above is not your cup of tea, I'd recommend swallowing the bitter pill :).

hazardous
  • 10,627
  • 2
  • 40
  • 52
  • Thanks for the answer. Well I'm disappointed because I *hate* calling getters just for getting a value. It feels like working with Java beans again... – vcarel May 06 '16 at 16:08
0

I think you are missing the concept Immutable was build:

Immutable data cannot be changed once created, leading to much simpler application development, no defensive copying, and enabling advanced memoization and change detection techniques with simple logic. Persistent data presents a mutative API which does not update the data in-place, but instead always yields new updated data.

One way or another you may transform Immutable data structures to plain old JS objects as: myMap.toJS()

vorillaz
  • 6,098
  • 2
  • 30
  • 46
  • 1
    IMHO the OP is concerned about the need of using get() methods to read members. The question is not about the set API or the mutation pattern used in Immutable. This topic has been a common concern with new adopters, as most often this results in embedding views with immutable code too. Immutable already has `Record` which uses JS getters to expose members first class to address this to a certain extent. The toJS conversions lose immutability properties of the model and hence are not always desired, say when you want to render children in a list, but still want to optimize component updates. – hazardous May 04 '16 at 12:29