Scope
There are a few libraries to extend Jotai's usage in React.
jotai-scope
While Jotai's Provider allows to scope Jotai's store under a subtree, we can't use the store above the tree within the subtree.
A workaround is to use store
option in useAtom and other hooks.
To ease such use cases, jotai-scope
provides a
scoped provider and hooks.
Instead of specifying the store
option,
the scoped provider accepts atoms
prop
and the use of those atoms is scoped within the subtree.
Install
npm install jotai-scope
Counter Example
import { atom, useAtom } from 'jotai'import { ScopeProvider } from 'jotai-scope'const countAtom = atom(0)const anotherCountAtom = atom(0)const Counter = () => {const [count, setCount] = useAtom(countAtom)const [anotherCount, setAnotherCount] = useAtom(anotherCountAtom)return (<><div><span>count: {count}</span><button type="button" onClick={() => setCount((c) => c + 1)}>increment</button></div><div><span>another count: {anotherCount}</span><button type="button" onClick={() => setAnotherCount((c) => c + 1)}>increment</button></div></>)}const App = () => {return (<div><h1>First Provider</h1><ScopeProvider atoms={[anotherCountAtom]}><Counter /></ScopeProvider><h1>Second Provider</h1><ScopeProvider atoms={[anotherCountAtom]}><Counter /></ScopeProvider></div>)}
createIsolation
Both Jotai's Provider and jotai-scope
's scoped provider
are still using global contexts.
If you are developing a library that depends on Jotai and the library user may use Jotai separately in their apps, they can share the same context. This can be troublesome because they point to unexpected Jotai stores.
To avoid conflicting the contexts,
a utility function called createIsolation
is exported from jotai-scope
.
Check out the example in the repo: https://github.com/jotaijs/jotai-scope/tree/main/examples/01_isolation
bunshi
(formerly jotai-molecules
)
Jotai atoms provide a basic solution to optimize re-renders. Atoms defined globally can depend on other atoms, but they can't depend on props and state within a component tree. It's possible to define atoms within a component tree, but then you would need to pass those atoms in some ways (for example, atoms-in-atom.)
bunshi is a third-party library to help such use cases.
See Motivation for more details.
Install
npm install bunshi
Counter Example
import { atom, useAtom } from 'jotai'import { molecule, useMolecule, createScope, ScopeProvider } from 'bunshi/react'const InitialCountScope = createScope(0)const countMolecule = molecule((getMol, getScope) => {const initialCont = getScope(InitialCountScope)return atom(initialCont)})const Counter = () => {const countAtom = useMolecule(countMolecule)const [count, setCount] = useAtom(countAtom)return (<div>{count} <button onClick={() => setCount((c) => c + 1)}>+1</button></div>)}const App = () => (<div><h1>With initial value 1</h1><ScopeProvider scope={InitialCountScope} value={1}><Counter /><Counter /></ScopeProvider><h1>With initial value 2</h1><ScopeProvider scope={InitialCountScope} value={2}><Counter /><Counter /></ScopeProvider><h1>Default</h1><Counter /><Counter /></div>)