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: old pages/concepts/signals.mdx
+30-24Lines changed: 30 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,39 +11,45 @@ tags:
11
11
version: '1.0'
12
12
---
13
13
14
-
Signals are the primary means of [managing state](/concepts/intro-to-reactivity#state-management) in your Solid application.
15
-
They provide a way to store and update values, and are the foundation of [reactivity](/concepts/intro-to-reactivity) in Solid.
14
+
Signals are the **core primitive for state** in Solid.
15
+
They provide a way to store a value, read it, and update it.
16
+
When a signal changes, anything that depends on it will update automatically.
16
17
17
-
Signals can be used to represent any kind of state in your application, such as the current user, the current page, or the current theme.
18
-
This can be any value, including primitive values such as strings and numbers, or complex values such as objects and arrays.
18
+
Signals can represent any kind of state in your application:
19
+
- simple values like numbers or strings
20
+
- complex values like objects or arrays
21
+
- application state such as the current user, theme, or page
22
+
23
+
For an overview of how signals fit into Solid’s reactive model, see [Reactivity Basics](/reactivity/basics).
19
24
20
25
## Creating a signal
21
26
22
-
You can create a signal by calling the [`createSignal`](/reference/basic-reactivity/create-signal) function, which is imported from `solid-js`.
23
-
This function takes an initial value as an argument, and returns a pair of functions: a **getter** function, and a **setter** function.
27
+
Use [`createSignal`](/reference/basic-reactivity/create-signal) from `solid-js` to create a signal.
28
+
It returns a pair of functions:
29
+
- a **getter** to read the value
30
+
- a **setter** to update the value
24
31
25
32
```jsx
26
33
import { createSignal } from"solid-js";
27
34
28
35
const [count, setCount] =createSignal(0);
29
-
// ^ getter ^ setter
36
+
// ^ getter ^ setter
30
37
```
31
38
32
-
:::note
33
-
The syntax using `[` and `]` is called [array destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment).
39
+
:::note
40
+
The syntax using `[` and `]` is called [array destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment).
34
41
35
42
This lets you extract values from the array.
36
43
In the context of `createSignal`, the first value is the getter function, and the second value is the setter function.
37
-
38
44
:::
39
45
40
46
## Accessing values
41
47
42
48
The getter function returned by `createSignal` is used to access the value of the signal.
43
-
You call this function with no arguments to get the current value of the signal:
49
+
Call the getter function with no arguments to read the current value:
44
50
45
51
```jsx
46
-
console.log(count()); //output: 0
52
+
console.log(count()); // 0
47
53
```
48
54
49
55
## Updating values
@@ -54,22 +60,21 @@ This function takes an argument that represents the new value of the signal:
54
60
```jsx
55
61
setCount(count() +1);
56
62
57
-
console.log(count()); //output: 1
63
+
console.log(count()); // 1
58
64
```
59
65
60
66
The setter function can also take a function that passes the previous value.
61
67
62
68
```jsx
63
69
setCount((prevCount) => prevCount +1);
64
70
65
-
console.log(count()); //output: 1
71
+
console.log(count()); // 1
66
72
```
67
73
68
74
## Reactivity
69
75
70
-
Signals are reactive, which means that they automatically update when their value changes.
71
-
When a signal is called within a [tracking scope](/concepts/intro-to-reactivity#tracking-changes), the signal adds the dependency to a list of subscribers.
72
-
Once a signal's value changes, it notifies all of its dependencies of the change so they can re-evaluate their values and update accordingly.
76
+
Signals are **reactive**, which means that they automatically update when their value changes.
77
+
For example, using a signal inside JSX automatically makes the DOM update when the signal changes:
73
78
74
79
```jsx
75
80
functionCounter() {
@@ -87,12 +92,13 @@ function Counter() {
87
92
}
88
93
```
89
94
90
-
:::note
91
-
A tracking scope can be created by [`createEffect`](/reference/basic-reactivity/create-effect) or [`createMemo`](/reference/basic-reactivity/create-memo), which are other Solid primitives.
92
-
93
-
Both functions subscribe to the signals accessed within them, establishing a dependency relationship.
94
-
Once this relationship is established, the function is notified whenever the signal changes.
95
+
To learn how signals connect to effects and tracking scopes, see:
96
+
-[Reactive Side Effects](/reactivity/effects)
97
+
-[Memoized Computations](/reactivity/memo)
95
98
96
-
:::
99
+
## Related Pages
97
100
98
-
To learn more about how to use Signals in your application, visit our [state management guide](/guides/state-management).
0 commit comments