SolidJSS
SolidJSโ€ข2y agoโ€ข
6 replies
skrebbel

Derived data in a store vs reconcile

Hey there, I'm trying to somehow have derived/computed data sit in a rather deeply nested store, ie values that depend on "real" values stored in the store. The docs suggests getters, and that works fine, until I try to use reconcile:

const [state, setState] = createStore({
    a: 5,
    b: 6,
    get c() { return state.a + state.b; }
});

setState(reconcile({ a: 7, b: 6 }));
console.log(state.c); //error, `c` got deleted


Our data is a particularly great fit for
reconcile
, it'd be a shame to not be able to use that. In the end I found a workaround, which is to make a field
data
with the all the raw data in it (and reconcile that), and put the getters one level higher in the hierarchy. Is that the recommended approach? Or is there a different way to have
reconcile
leave the setters be?

I noticed that with a class (returning createMutable(this) from the constructor) it actually works, ie I can put instances of that class in a store and reconcile on the fields, and the getters/methods aren't touched because they're on the prototype and not the instance. But I don't love it because, even though I really like classes/OO, I don't really love making them fully mutable for all the reasons Ryan likes to cite. I'd totally go for an immutable class, F# with style, but I've yet to find an elegant way to accomplish that with JS (let alone solid stores / reconcile).

Any ideas? Thanks!
Was this page helpful?