flummox icon indicating copy to clipboard operation
flummox copied to clipboard

Want to use immutable-js map as store, but can't

Open Termina1 opened this issue 10 years ago • 8 comments

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?

Termina1 avatar Apr 08 '15 12:04 Termina1

Yeah, we need to replace assign() with this.constructor.assignState().

acdlite avatar Apr 08 '15 13:04 acdlite

+1

SimonDegraeve avatar Apr 20 '15 02:04 SimonDegraeve

+1

rn0 avatar Apr 20 '15 07:04 rn0

+1

athibaud avatar Apr 23 '15 15:04 athibaud

+1

babsonmatt avatar Apr 26 '15 01:04 babsonmatt

any update on this ?

boopathi avatar May 17 '15 13:05 boopathi

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

chrisvariety avatar May 17 '15 15:05 chrisvariety

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.

boopathi avatar May 17 '15 15:05 boopathi