Skip to content

Commit

Permalink
feat(withAtomEffect): add withAtomEffect to export
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Sep 16, 2024
1 parent fa0f231 commit 6c52675
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -92,7 +92,7 @@ function MyComponent() {

</details>

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

<!-- prettier-ignore -->
Expand Down Expand Up @@ -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

Expand All @@ -355,26 +355,26 @@ declare function withAtomEffect<T>(
): Atom<T>
```

**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
Expand Down
2 changes: 2 additions & 0 deletions __tests__/withAtomEffect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jotai-effect",
"description": "👻🔁",
"version": "1.0.2",
"version": "1.0.3",
"author": "David Maskasky",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { atomEffect } from './atomEffect'
export { withAtomEffect } from './withAtomEffect'

0 comments on commit 6c52675

Please sign in to comment.