IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

Creating collection inside computed fails with [mobx] Side effects like changing state are not allowed at this point. #34

Closed Strate closed 5 years ago

Strate commented 6 years ago

Hey!

I'm trying to make something like that:

const collection = mobx.computed(() => new firestorter.Collection( /* ... */ ));

const Component = mobxReact.observer(function Component() {
  return <div>{collection.get().docs.length}</div>
})

This is fails with error:

Error: [mobx] Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: ObservableValue@23
    at invariant (webpack:///./node_modules/mobx/lib/mobx.module.js?:2705:15)
    at fail$1 (webpack:///./node_modules/mobx/lib/mobx.module.js?:2700:5)
    at checkIfStateModificationsAreAllowed (webpack:///./node_modules/mobx/lib/mobx.module.js?:3332:9)
    at ObservableValue.prepareNewValue (webpack:///./node_modules/mobx/lib/mobx.module.js?:999:9)
    at ObservableValue.set (webpack:///./node_modules/mobx/lib/mobx.module.js?:982:25)
    at Collection._updateRealtimeUpdates (webpack:///./node_modules/firestorter/dist/firestorter.module.js?:3315:15)
    at eval (webpack:///./node_modules/firestorter/dist/firestorter.module.js?:3166:13)
    at transaction (webpack:///./node_modules/mobx/lib/mobx.module.js?:1660:23)
    at Collection.addObserverRef (webpack:///./node_modules/firestorter/dist/firestorter.module.js?:3165:61)
    at AtomImpl.atom.reportObserved (webpack:///./node_modules/firestorter/dist/firestorter.module.js?:117:13)

Seems that addObserverRef performs mutation of observable, which is not allowed in mobx. As for now, I found workaround to wrap new firestorter.Collection in mobx.untracked:

const collection = mobx.computed(mobx.untracked(() => new firestorter.Collection( /* ... */ )));

but I am not sure this is not breaking something, for example reactive query, or other stuff.

IjzerenHein commented 6 years ago

Hi, thanks for pointing this out. There are at this point some "impurities" with firestorter. I'm planning on rewriting a thing or two to make everything as pure as possible. I don't have a solution for this at this moment but will come back to you in a while. cheers

IjzerenHein commented 6 years ago

Hi, this should be fixed in the new v1.1.0 release. Let me know if it doesn't. cheers