goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

How to handle a dispatch related to an ES6 Map on the store? #662

Open afilp opened 8 years ago

afilp commented 8 years ago

Which is the "correct" way to change an ES6 Map on the store?

Note that this.props.mapAlertsDoNotShowAgain is coming from AltContainer as a reference to the related value in the altjs store.

Version 1: (has a small performance hit due to the cloning)

const mapAlertsDoNotShowAgain = new Map(this.props.mapAlertsDoNotShowAgain);
        mapAlertsDoNotShowAgain.set(mapAlertType, 3);
        uiActions.changeValue({
            which: 'mapAlertsDoNotShowAgain',
            value: mapAlertsDoNotShowAgain,
        });

OR

Version 2: (mutates the reference and then does the action, we do not clone as the end result will be the same... or NOT?)

        this.props.mapAlertsDoNotShowAgain.set(mapAlertType, 3);
        uiActions.changeValue({
            which: 'mapAlertsDoNotShowAgain',
            value: mapAlertsDoNotShowAgain,
        });

In other words, should we always clone an Array, Object, Map in order to change one of their values through an action? Or we can just mutate their reference and then send the action?

Thanks for the clarification.