Replies: 2 comments 1 reply
-
Not 100% sure, but it sounds close to Bunshi's goal. |
Beta Was this translation helpful? Give feedback.
-
Circling back just to share my findings here. There are a few API things that keep it from working exactly how i like but that can be solved w/ some custom wrappers.
One limitation I ran into which had me pursue a different approach though was I couldn't seem to figure out how to subscribe to the molecule without using the react/vue layers. I wanted my result object from my helper to still be a |
Beta Was this translation helpful? Give feedback.
-
Hey @dai-shi (and the community)
I was hoping to get a sanity check on an approach for injecting atoms into my sub modules.
General Problem Context
Here is a contrived example
This example is intentionally simple but in practice these derived atoms could be much more complex, joining data from multiple sources etc.
So if we want to move
src/component_at.ts
into its own package (pnpm in this example) with this target organizationWe would be unable to import the
userAtom
incomponent_a.ts
without creating a dependency cycle. One approach to resolve this is to create another package forsession.ts
like this.Then our dependency graph can look like this
This all works fine and as expected, but has a few downsides
session
to modularize aPossible Alternative
The goal here isn't to say if one is better than the other but to sanity check the alternative for potential issues or oversights.
Can we create a helper to build a
createJotaiContext
function that allows us to define some global jotai state containers but set their sources whencomponent_a.ts
mounts.This can be derived from like this
The boundary component needs to mount the Provider and supply these via props
How it works
Under the hood we just create an
PrimitiveAtom<Atom<V>>
for each key of the context you defined so for this example we made aPrimitiveAtom<Atom<User>>
. The provider sets the backingAtom<User>
from the props provided usinguseStore
and.set
Concerns / Issues I see
UserContext.Provider
with different backing atoms could cause thrash / issuesBenefits I see
Overall not super sure how I feel about it but figured you or others might have good insight here and would love your thoughts
Beta Was this translation helpful? Give feedback.
All reactions