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: README.md
+40-10
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ yarn add @ibnlanre/signals
14
14
15
15
## Usage
16
16
17
-
Within a React component, it is advisable to create a signal using the `signal` function. The `signal` function takes an initial value and returns a signal object. The signal object has a `use` method that returns the current value and a setter function.
17
+
It is advisable to create a signal using the `signal` function, outside of a React component to avoid re-creating the signal on every render. The signal object can then be used within a React component using the `use` method. The `signal` function takes an initial value and returns a signal object, while the `use` method returns the current value and a setter function.
18
18
19
19
### Within a React component
20
20
@@ -29,7 +29,9 @@ function Counter() {
29
29
return (
30
30
<div>
31
31
<p>{countValue}</p>
32
-
<button onClick={() => { setCount(c=>++c) }}>
32
+
<button onClick={() => {
33
+
setCount(prevCount=> prevCount +1)
34
+
}}>
33
35
Increment
34
36
</button>
35
37
</div>
@@ -39,12 +41,15 @@ function Counter() {
39
41
40
42
### Outside a React component
41
43
42
-
Outside a React component, you can use the signal object directly. It could also be used within, but accessing the `.value` property directly is not recommended, as the value will not be updated when the signal changes, without a re-render.
44
+
Outside a React component, you can use the signal object directly. It could also be used within, but accessing the `.value` property directly is not recommended, as the value will not be updated when the signal changes, without a re-render. The issue of the value not updating does not exist outside a React component.
43
45
44
46
```typescript
45
47
const count =signal(0);
46
48
47
-
const changeCount = () =>count.value++;
49
+
const increment = () =>count.value++;
50
+
const decrement = () => {
51
+
count.value=count.value-1
52
+
};
48
53
```
49
54
50
55
### Computed signals
@@ -55,7 +60,28 @@ It is also possible to create a `computed` signal, which depends on other signal
55
60
import { computed } from'@ibnlanre/signals';
56
61
57
62
const count =signal(0);
58
-
const doubleCount =computed([c], () =>c.value*2);
63
+
const doubleCount =computed(() =>c.value*2, [c]);
64
+
```
65
+
66
+
A computed signal can be used in the same way as a regular signal. It can be used within a React component using the `use` method, or outside a React component using the `value` property. But it cannot be updated directly. Rather its value is derived from the signals it depends on.
The `effect` method of the signal object is used to run a function whenever the signal changes. It takes a function and an optional `immediate`parameter, which is `true` by default.
123
+
The `subscribe` method of the signal object is used to run a function whenever the signal changes. It takes a function and an optional `immediate`argument, which is `true` by default.
98
124
99
125
```typescript
100
-
count.effect((value) => {
126
+
count.subscribe((value) => {
101
127
console.log('count changed:', value);
102
128
}, false);
103
129
```
104
130
105
-
### `watch`
131
+
### `effect`
106
132
107
-
The `watch` method of the signal object is used to run a function whenever the signal changes. It takes a function and an optional `immediate`parameter, which is `true` by default.
133
+
The `effect` method of the signal object performs the same function as the `subscribe` method, but it should be used within a React component, as it is implemented using the `useEffect` hook. It takes a function and an optional `immediate`argument, which is `true` by default. While the `subscribe` method could be used within a React component, the `effect` method is recommended, as it is automatically cleaned up when the component is unmounted.
0 commit comments