hally9k hally9k - 1 year ago 367
Javascript Question

Why does Immutable.js throw Invalid key path on Map.setIn()

I must be missing something here because the Docs make it out as if the below code should work just fine but I get an invalid keypath error... Check this codepen.

var map1 = Immutable.Map({ 'selector': { 'type': 'bar' }});
var map2 = map1.setIn(['selector', 'type'], 'foo');

Answer Source

This happens because the key 'selector' has a non-Map value. Instead you need:

var map1 = Immutable.Map({ 'selector': Immutable.Map({ 'type': 'bar' })});
var map2 = map1.setIn(['selector', 'type'], 'foo');
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>