Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(effect): add section for get.peek api and clean up wording #2374

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions docs/extensions/effect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ npm i jotai-effect
type CleanupFn = () => void

type EffectFn = (
get: Getter,
get: Getter & { peek: Getter },
set: Setter & { recurse: Setter },
) => CleanupFn | void

function atomEffect(effectFn: EffectFn): Atom<void>
declare function atomEffect(effectFn: EffectFn): Atom<void>
```

**effectFn** (required): A function for listening to state updates with `get` and writing state updates with `set`. The `effectFn` is useful for creating side effects that interact with other Jotai atoms. You can cleanup these side effects by returning a cleanup function.
Expand Down Expand Up @@ -98,7 +98,7 @@ function MyComponent() {
</details>

- **Resistent To Infinite Loops:**
`atomEffect` does not rerun when it changes a value that it is watching with `set`.
`atomEffect` does not rerun when it changes a value with `set` that it is watching.

<!-- prettier-ignore -->
<details style="cursor: pointer; user-select: none;">
Expand Down Expand Up @@ -136,6 +136,23 @@ function MyComponent() {

</details>

- **Supports Peek:**
Read atom data without subscribing to changes with `get.peek`.

<!-- prettier-ignore -->
<details style="cursor: pointer; user-select: none;">
<summary>Example</summary>

```js
const countAtom = atom(0)
atomEffect((get, set) => {
// will not rerun when countAtom changes
const count = get.peek(countAtom)
})
```

</details>

- **Executes In The Next Microtask:**
`effectFn` runs in the next available microtask, after all Jotai synchronous read evaluations have completed.

Expand Down
Loading