From 6c526756d59ee31a10c3e370f3ac507aa966b7b0 Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Fri, 30 Aug 2024 10:38:58 -0700 Subject: [PATCH] feat(withAtomEffect): add withAtomEffect to export --- README.md | 22 +++++++++++----------- __tests__/withAtomEffect.test.ts | 2 ++ package.json | 2 +- src/index.ts | 1 + 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index df52d49..cd02bfc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Effect -[jotai-effect](https://jotai.org/docs/integrations/effect) is a utility package for reactive side effects. +[jotai-effect](https://jotai.org/docs/extensions/effect) is a utility package for reactive side effects. ## install @@ -92,7 +92,7 @@ function MyComponent() { -- **Resistent To Infinite Loops:** +- **Resistant To Infinite Loops:** `atomEffect` does not rerun when it changes a value with `set` that it is watching. @@ -344,7 +344,7 @@ Aside from mount events, the effect runs when any of its dependencies change val ## withAtomEffect -`withAtomEffect` is a utility to define an effect that is bound to the target atom. This is useful for creating effects that are active when the target atom is mounted. +`withAtomEffect` binds an effect to a clone of the target atom. This is useful for creating effects that are active when the clone of the target atom is mounted. ### Parameters @@ -355,26 +355,26 @@ declare function withAtomEffect( ): Atom ``` -**targetAtom** (required): The atom to which the effect is mounted. +**targetAtom** (required): The atom to which the effect is bound. **effectFn** (required): A function for listening to state updates with `get` and writing state updates with `set`. -**Returns:** An atom that is equivalent to the target atom but with the effect mounted. +**Returns:** An atom that is equivalent to the target atom but having a bound effect. ### Usage ```js import { withAtomEffect } from 'jotai-effect' -const anAtom = atom(0) -const loggingAtom = withAtomEffect(anAtom, (get, set) => { - // runs on mount or whenever anAtom changes - const value = get(anAtom) - loggingService.setValue(value) +const valuesAtom = withAtomEffect(atom(null), (get, set) => { + // runs when valuesAtom is mounted + const unsubscribe = subscribe((value) => { + set(valuesAtom, value) + }) + return unsubscribe }) ``` - ## Comparison with useEffect ### Component Side Effects diff --git a/__tests__/withAtomEffect.test.ts b/__tests__/withAtomEffect.test.ts index 73138f5..13aeca8 100644 --- a/__tests__/withAtomEffect.test.ts +++ b/__tests__/withAtomEffect.test.ts @@ -22,6 +22,8 @@ describe('withAtomEffect', () => { store.sub(enhancedAtom, () => {}) store.set(enhancedAtom, 5) expect(store.get(enhancedAtom)).toBe(5) + store.set(enhancedAtom, (prev) => prev + 1) + expect(store.get(enhancedAtom)).toBe(6) }) it('calls effect on initial use and on dependencies change', async () => { diff --git a/package.json b/package.json index 1e815b5..61f6f4e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jotai-effect", "description": "👻🔁", - "version": "1.0.2", + "version": "1.0.3", "author": "David Maskasky", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 119d5fa..7d7c89b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export { atomEffect } from './atomEffect' +export { withAtomEffect } from './withAtomEffect'