Skip to content

Commit 63e122d

Browse files
committed
update
1 parent 4045d18 commit 63e122d

1 file changed

Lines changed: 18 additions & 27 deletions

File tree

  • src/routes/reference/reactive-utilities

src/routes/reference/reactive-utilities/batch.mdx

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Returns the value produced by `fn`.
4949
- Downstream computations are deferred until the batch completes.
5050
- Nested `batch` calls behave like a single larger batch.
5151
- If you read a stale memo or signal inside the batch, Solid updates it on demand before returning the value.
52-
- If `fn` is asynchronous, only the updates before the first `await` are batched.
52+
- If `fn` is asynchronous, batching applies only to updates before the first `await` or other async suspension point.
5353

5454
## Automatic batching
5555

@@ -61,49 +61,40 @@ Solid automatically batches updates in several cases, including:
6161

6262
## Examples
6363

64-
### Batch multiple signal updates
64+
### Basic usage
6565

6666
```ts
67-
import { batch, createEffect, createMemo, createSignal } from "solid-js";
67+
const [count, setCount] = createSignal(0);
68+
const [total, setTotal] = createSignal(0);
6869

69-
const [up1, setUp1] = createSignal(1);
70-
const [up2, setUp2] = createSignal(2);
71-
const [up3, setUp3] = createSignal(3);
72-
73-
const down = createMemo(() => up1() + up2() + up3());
74-
createEffect(() => console.log(down())); // outputs 6
70+
const summary = createMemo(() => `${count()} / ${total()}`);
71+
createEffect(() => console.log(summary())); // logs "0 / 0"
7572
```
7673

77-
Without `batch`, each setter can trigger downstream work:
74+
Outside `batch`:
7875

7976
```ts
80-
setUp1(4); // recomputes down, outputs 9
81-
setUp2(5); // recomputes down, outputs 12
82-
setUp3(6); // recomputes down, outputs 15
77+
setCount(1); // logs "1 / 0"
78+
setTotal(5); // logs "1 / 5"
8379
```
8480

85-
With `batch`, the memo recomputes once at the end:
81+
Inside `batch`:
8682

8783
```ts
8884
batch(() => {
89-
setUp1(10); // doesn't update down yet
90-
setUp2(10); // doesn't update down yet
91-
setUp3(10); // doesn't update down yet
92-
}); // recomputes down, outputs 30
85+
setCount(1);
86+
setTotal(5);
87+
}); // logs "1 / 5"
9388
```
9489

95-
### Read a memo inside a batch
90+
### Read inside a batch
9691

9792
```ts
9893
batch(() => {
99-
setUp1(11); // doesn't update down yet
100-
setUp2(11); // doesn't update down yet
101-
setUp3(11); // doesn't update down yet
102-
console.log(down()); // recomputes down, outputs 33
103-
setUp1(12); // doesn't update down yet
104-
setUp2(12); // doesn't update down yet
105-
setUp3(12); // doesn't update down yet
106-
}); // recomputes down, outputs 36
94+
setCount(2);
95+
console.log(summary()); // logs "2 / 5"
96+
setTotal(10);
97+
}); // logs "2 / 10"
10798
```
10899

109100
## Related

0 commit comments

Comments
 (0)