You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-16Lines changed: 21 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,19 @@
2
2
3
3
👻🔭 *Isolate Jotai atoms with scope*
4
4
5
+
### Install
6
+
7
+
```
8
+
npm install jotai-scope
9
+
```
10
+
5
11
## ScopeProvider
6
12
7
-
`<ScopeProvider>` lets you reuse the *same* atoms in different parts of the React tree **without sharing state** while still being able to read other atoms from the parent store.
13
+
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.
8
14
9
-
---
15
+
A workaround is to use `store` option in useAtom and other hooks.
16
+
17
+
Instead of specifying the `store` option, `<ScopeProvider>` lets you reuse the *same* atoms in different parts of the React tree **without sharing state** while still being able to read other atoms from the parent store.
10
18
11
19
### At‑a‑glance
12
20
@@ -16,8 +24,6 @@
16
24
***Nested lookup.** If a scope can’t find the atom in the current scope, it inherits from the nearest parent scope, up to the nearest store.
17
25
* Scoping works for both reading from atoms *and* writing to atoms.
18
26
19
-
---
20
-
21
27
### Quick Start
22
28
23
29
```tsx
@@ -56,8 +62,6 @@ export default function App() {
56
62
57
63
The second counter owns a private `doubledAtom`*and* a private `countAtom` because `doubledAtom` is scoped.
58
64
59
-
---
60
-
61
65
**2 · Nested scopes**
62
66
63
67
```tsx
@@ -72,8 +76,6 @@ The second counter owns a private `doubledAtom` *and* a private `countAtom` b
72
76
* Outer scope (S1) isolates `countAtom`.
73
77
* Inner scope (S2) isolates `nameAtom`, then looks up the tree and finds `countAtom` in S1.
74
78
75
-
---
76
-
77
79
**3 · Providing default values**
78
80
79
81
```tsx
@@ -84,8 +86,6 @@ The second counter owns a private `doubledAtom` *and* a private `countAtom` b
84
86
85
87
Mix tuples and plain atoms as needed: `atoms={[[countAtom, 1], anotherAtom]}`.
Inside the `<ScopeProvider>` every `itemFamily(id)` call resolves to a scoped copy, so items rendered inside the provider are independent from the global ones and from any sibling scopes.
* Avoid side effects inside atom read—it may run multiple times per scope. For async atoms, use an abort controller. The extra renders are a known limitation and solutions are being researched. If you are interested in helping, please [join the discussion](https://github.com/jotaijs/jotai-scope/issues/25).
`createIsolation` is useful for library authors to create a private store for libraries that use Jotai. It provides a private `Provider` and hooks that do not share state with the global store.
140
+
Both Jotai's Provider and `jotai-scope`'s scoped provider
141
+
are still using global contexts.
142
+
143
+
If you are developing a library that depends on Jotai and
144
+
the library user may use Jotai separately in their apps,
145
+
they can share the same context. This can be troublesome
146
+
because they point to unexpected Jotai stores.
147
+
148
+
To avoid conflicting the contexts, a utility function called `createIsolation` is exported from `jotai-scope`.
0 commit comments