Skip to content

Commit

Permalink
docs(scope): Add tips gathered from common issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yf-yang committed Apr 30, 2024
1 parent b3ba69c commit 90f1082
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,28 @@
👻🔭

https://jotai.org/docs/integrations/scope

## Pro Tips

1. Within a `ScopeProvider`, although an atom may not be scoped, its `atom.read` function could be called multiple times. **Therefore, do not use `atom.read` to perform side effects.**

NOTE: Async atoms always have side effects. To handle it, add additional code to prevent extra side effects. You can check this [issue](https://github.com/jotaijs/jotai-scope/issues/25#issuecomment-2014498893) as an example.

2. Scoping a derived atom has no behavioral effect. Avoid it.

Derived atoms are merely proxies of primitive atoms. When scoping a derived atom, although the atom itself is copied, all of those copies still reference the same primitive atom, so their values are still shared.

It is a common pattern in Jotai to wrap the primitive atom within a utility function. To scope it properly, you should expose the primitive atom and then apply the scoping to it.

``` javascript
function someAtomUtility(initialValue) {
// This is the primitive atom. Return it and then scope it.
const anAtom = atom(initialValue)

// Be careful, this is a derived atom! Scoping it has no effect.
return atom(
(get) => get(anAtom),
(get, set, update) => set(anAtom, update)
),
}
```

0 comments on commit 90f1082

Please sign in to comment.