Skip to content

Commit 6f8f8d3

Browse files
authored
docs: add documentation section about nested effects (#98)
1 parent 6361b0d commit 6f8f8d3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,33 @@ stopScope();
9797
count(3); // No console output
9898
```
9999

100+
#### Nested Effects
101+
102+
Effects can be nested inside other effects. When the outer effect re-runs, inner effects from the previous run are automatically cleaned up, and new inner effects are created if needed. The system ensures proper execution order, outer effects always run before their inner effects:
103+
104+
```ts
105+
import { signal, effect } from 'alien-signals';
106+
107+
const show = signal(true);
108+
const count = signal(1);
109+
110+
effect(() => {
111+
if (show()) {
112+
// This inner effect is created when show() is true
113+
effect(() => {
114+
console.log(`Count is: ${count()}`);
115+
});
116+
}
117+
}); // Console: Count is: 1
118+
119+
count(2); // Console: Count is: 2
120+
121+
// When show becomes false, the inner effect is cleaned up
122+
show(false); // No output
123+
124+
count(3); // No output (inner effect no longer exists)
125+
```
126+
100127
#### Manual Triggering
101128

102129
The `trigger()` function allows you to manually trigger updates for downstream dependencies when you've directly mutated a signal's value without using the signal setter:

0 commit comments

Comments
 (0)