flummox
flummox copied to clipboard
Want to use immutable-js map as store, but can't
This is the line, where Map becomes broken https://github.com/acdlite/flummox/blob/master/src/addons/reactComponentMethods.js#L75.
And it seems, I can't anyhow configure this behaviour?
Yeah, we need to replace assign() with this.constructor.assignState().
+1
+1
+1
+1
any update on this ?
In case you all didn't know, right now you can easily use Map within a store as seen on the docs sample app: https://github.com/acdlite/flummox/blob/master/docs/src/shared/stores/DocStore.js#L12
I think most of the solution is already here :smiley: https://github.com/acdlite/flummox-immutable-store
Looks like setState internally calls constructor.assignState, and you can simply override that to match how your Immutable object would change (/ merge).
import {Record} from 'immutable';
class MyStore extends Store {
constructor() {
const StateRecord = Record({ ...stateObj });
// Set your immutable state here
this.state = new StateRecord();
}
// define how your newState affects your oldState and return merged state
static assignState(oldState, newState) {
if (oldState === undefined || oldState === null) {
const StateRecord = newState.constructor;
oldState = new StateRecord();
}
return oldState.merge(oldState, newState);
}
handler() {
// and you can just use,
this.setState( this.state.set('prop', 'value') );
}
}
I think, you can do that to Immutable.Map in a similar way.