You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/guide/v10/signals.md
+268Lines changed: 268 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -498,6 +498,51 @@ To enable this optimization, pass the signal into JSX instead of accessing its `
498
498
499
499
A similar rendering optimization is also supported when passing signals as props on DOM elements.
500
500
501
+
## Models
502
+
503
+
Models provide a structured way to build reactive state containers that encapsulate signals, computed values, effects, and actions. They offer a clean pattern for organizing complex state logic while ensuring automatic cleanup and batched updates.
504
+
505
+
As applications grow in complexity, managing state with individual signals can become unwieldy. Models solve this by bundling related signals, computed values, and actions together into cohesive units. This makes your code more maintainable, testable, and easier to reason about.
506
+
507
+
### Why Use Models?
508
+
509
+
Models offer several key benefits:
510
+
511
+
-**Encapsulation**: Group related state and logic together, making it clear what belongs where
512
+
-**Automatic cleanup**: Effects created in models are automatically disposed when the model is disposed, preventing memory leaks
513
+
-**Automatic batching**: All methods are automatically wrapped as actions, ensuring optimal performance
514
+
-**Composability**: Models can be nested and composed, with parent models automatically managing child model lifecycles
515
+
-**Reusability**: Models can accept initialization parameters, making them reusable across different contexts
516
+
-**Testability**: Models can be instantiated and tested in isolation without requiring component rendering
517
+
518
+
Here's a simple example showing how models organize state:
For more details on how to use models in your components and the full API reference, see the [Model APIs](#createmodelfactory) in the API section below.
545
+
501
546
## API
502
547
503
548
This section is an overview of the signals API. It's aimed to be a quick reference for folks who already know how to use signals and need a reminder of what's available.
@@ -604,6 +649,229 @@ effect(() => {
604
649
});
605
650
```
606
651
652
+
### createModel(factory)
653
+
654
+
The `createModel(factory)` function creates a model constructor from a factory function. The factory function can accept arguments for initialization and should return an object containing signals, computed values, and action methods.
counter.increment(); // Updates are automatically batched
682
+
console.log(counter.count.value); // 6
683
+
console.log(counter.doubled.value); // 12
684
+
685
+
// Clean up all effects when done
686
+
counter[Symbol.dispose]();
687
+
```
688
+
689
+
#### Key Features
690
+
691
+
-**Factory arguments**: Factory functions can accept arguments for initialization, making models reusable with different configurations.
692
+
-**Automatic batching**: All methods returned from the factory are automatically wrapped as actions, meaning state updates within them are batched and untracked.
693
+
-**Automatic effect cleanup**: Effects created during model construction are captured and automatically disposed when the model is disposed via `Symbol.dispose`.
694
+
-**Composable models**: Models compose naturally - effects from nested models are captured by the parent and disposed together when the parent is disposed.
695
+
696
+
#### Model Composition
697
+
698
+
Models can be nested within other models. When a parent model is disposed, all effects from nested models are automatically cleaned up:
699
+
700
+
```js
701
+
constTodoItemModel=createModel((text) => {
702
+
constcompleted=signal(false);
703
+
704
+
return {
705
+
text,
706
+
completed,
707
+
toggle() {
708
+
completed.value=!completed.value;
709
+
}
710
+
};
711
+
});
712
+
713
+
constTodoListModel=createModel(() => {
714
+
constitems=signal([]);
715
+
716
+
return {
717
+
items,
718
+
addTodo(text) {
719
+
consttodo=newTodoItemModel(text);
720
+
items.value= [...items.value, todo];
721
+
},
722
+
removeTodo(todo) {
723
+
items.value=items.value.filter(t=> t !== todo);
724
+
todo[Symbol.dispose]();
725
+
}
726
+
};
727
+
});
728
+
729
+
consttodoList=newTodoListModel();
730
+
todoList.addTodo('Buy groceries');
731
+
todoList.addTodo('Walk the dog');
732
+
733
+
// Disposing the parent also cleans up all nested model effects
734
+
todoList[Symbol.dispose]();
735
+
```
736
+
737
+
### action(fn)
738
+
739
+
The `action(fn)` function wraps a function to run in a batched and untracked context. This is useful when you need to create standalone actions outside of a model:
740
+
741
+
```js
742
+
import { signal, action } from'@preact/signals';
743
+
744
+
constcount=signal(0);
745
+
746
+
constincrementBy=action((amount) => {
747
+
count.value+= amount;
748
+
});
749
+
750
+
incrementBy(5); // Batched update
751
+
```
752
+
753
+
### useModel(modelOrFactory)
754
+
755
+
The `useModel` hook is available in both `@preact/signals` and `@preact/signals-react` packages. It handles creating a model instance on first render, maintaining the same instance across re-renders, and automatically disposing the model when the component unmounts.
counter.count.value=10; // TypeScript error: Cannot assign to 'value'
836
+
```
837
+
838
+
#### Custom Dispose Logic
839
+
840
+
If your model needs custom cleanup logic that isn't related to signals (such as closing WebSocket connections), use an effect with no dependencies that returns a cleanup function:
841
+
842
+
```js
843
+
constWebSocketModel=createModel((url) => {
844
+
constmessages=signal([]);
845
+
constws=newWebSocket(url);
846
+
847
+
ws.onmessage= (e) => {
848
+
messages.value= [...messages.value, e.data];
849
+
};
850
+
851
+
// This effect runs once; its cleanup runs on dispose
This pattern mirrors `useEffect(() => { return cleanup }, [])` in React and ensures that cleanup happens automatically when models are composed together - parent models don't need to know about the dispose functions of nested models.
874
+
607
875
## Utility Components and Hooks
608
876
609
877
As of v2.1.0, the `@preact/signals/utils` package provides additional utility components and hooks to make working with signals even easier.
0 commit comments