Skip to content

Commit

Permalink
docs: document effect cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide committed Mar 15, 2024
1 parent 5fca7b3 commit 590a466
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ dispose();
surname.value = "Doe 2";
```

The effect callback may return a cleanup function. The cleanup function gets run once, either when the effect callback is next called _or_ when the effect gets disposed, whichever happens first.

```js
import { signal, effect } from "@preact/signals-core";

const count = signal(0);

const dispose = effect(() => {
const c = count.value;
return () => console.log(`cleanup ${c}`);
});

// Logs: cleanup 0
count.value = 1;

// Logs: cleanup 1
dispose();
```

### `batch(fn)`

The `batch` function allows you to combine multiple signal writes into one single update that is triggered at the end when the callback completes.
Expand Down

0 comments on commit 590a466

Please sign in to comment.