Skip to content

Commit 18cd543

Browse files
committed
Refactor signal creation and usage in README.md
1 parent 6480e1c commit 18cd543

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

README.md

+40-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ yarn add @ibnlanre/signals
1414

1515
## Usage
1616

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.
1818

1919
### Within a React component
2020

@@ -29,7 +29,9 @@ function Counter() {
2929
return (
3030
<div>
3131
<p>{countValue}</p>
32-
<button onClick={() => { setCount(c => ++c) }}>
32+
<button onClick={() => {
33+
setCount(prevCount => prevCount + 1)
34+
}}>
3335
Increment
3436
</button>
3537
</div>
@@ -39,12 +41,15 @@ function Counter() {
3941

4042
### Outside a React component
4143

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.
4345

4446
```typescript
4547
const count = signal(0);
4648

47-
const changeCount = () => count.value++;
49+
const increment = () => count.value++;
50+
const decrement = () => {
51+
count.value = count.value - 1
52+
};
4853
```
4954

5055
### Computed signals
@@ -55,7 +60,28 @@ It is also possible to create a `computed` signal, which depends on other signal
5560
import { computed } from '@ibnlanre/signals';
5661

5762
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.
67+
68+
```jsx
69+
function DoubleCounter() {
70+
const [doubleCountValue, setDoubleCountValue] = doubleCount.use();
71+
72+
useEffect(() => {
73+
console.log('doubleCount changed:', doubleCountValue);
74+
}, [doubleCountValue]);
75+
76+
return (
77+
<div>
78+
<p>{doubleCountValue}</p>
79+
<button onClick={() => count.value--}>
80+
Decrement
81+
</button>
82+
</div>
83+
);
84+
}
5985
```
6086

6187
## API
@@ -92,21 +118,25 @@ The `computed` function creates a computed signal. It takes a function that retu
92118
const doubleCount = computed([count], () => count.value * 2);
93119
```
94120

95-
### `effect`
121+
### `subscribe`
96122

97-
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.
98124

99125
```typescript
100-
count.effect((value) => {
126+
count.subscribe((value) => {
101127
console.log('count changed:', value);
102128
}, false);
103129
```
104130

105-
### `watch`
131+
### `effect`
106132

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.
108134

109135
```typescript
136+
count.effect((value) => {
137+
console.log('count value:', value);
138+
});
139+
```
110140

111141
## License
112142

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ibnlanre/signals",
33
"version": "0.0.1",
4-
"description": "A simple reactive state management library built with signals",
4+
"description": "A simple reactive state management library for React, built with hooks",
55
"homepage": "https://github.com/ibnlanre/signals#readme",
66
"type": "module",
77
"scripts": {
@@ -63,4 +63,4 @@
6363
"typescript": "^5.4.5",
6464
"vitest": "^1.5.0"
6565
}
66-
}
66+
}

src/computed/computed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class Computed<
5959
/**
6060
* Creates a new signal with an optional initial value.
6161
*
62-
* @param dependencyList The list of signals that this signal depends on.
6362
* @param initialValue The initial value of the signal.
63+
* @param dependencyList The list of signals that this signal depends on.
6464
*
6565
* @returns The new signal.
6666
*/

tsconfig.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
"module": "CommonJS",
66
"baseUrl": ".",
77
"outDir": "./dist",
8-
"paths": {
9-
"@ibnlanre/*": [
10-
"./src",
11-
],
12-
},
138
"esModuleInterop": true,
149
"forceConsistentCasingInFileNames": true,
1510
"strict": true,
@@ -18,6 +13,6 @@
1813
"noUnusedParameters": false
1914
},
2015
"include": [
21-
"src/**/*.ts"
16+
"src/**/*.ts",
2217
]
2318
}

vitest.config.js vitest.config.ts

File renamed without changes.

0 commit comments

Comments
 (0)