bunshi icon indicating copy to clipboard operation
bunshi copied to clipboard

Not calling `getScope` in molecule definition results in shared atoms

Open mutewinter opened this issue 3 years ago • 4 comments

I noticed that if I don't call the getScope function during the creation of a molecule the atoms end up being shared on useMolecule. I'm also using both the ScopeProvider and useMolecule(Molecule, { withScope: [Scope, value]}); syntax, if that is an issue.

Loving the library. Feel like this is solving a real issue with Jotai that should perhaps someday be in core.

mutewinter avatar Aug 13 '22 17:08 mutewinter

Thanks @mutewinter, that is operating as intended. Calling getScope or getMolecule is what creates the dependency tree, similar to calling get in an atom.

loganvolkers avatar Aug 15 '22 14:08 loganvolkers

Gotcha. Feels like there should be a warning / error if it’s not called — if possible.

mutewinter avatar Aug 15 '22 18:08 mutewinter

I don't think it's possible to know a priori if a molecule should be scoped or not without that info. If you're using a global molecule (one without any calls to getMolecule or getScope) it is intended for it to be a singleton instance.

This could be better explained in the docs.

loganvolkers avatar Aug 15 '22 19:08 loganvolkers

Yeah, docs makes sense. The examples re-using the molecule value to initialize an atom felt like a contrived example and not a real requirement.

mutewinter avatar Aug 21 '22 13:08 mutewinter