@hoosk/minisignals-react 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/package.json +1 -1
- package/src/index.ts +0 -2
- package/src/useSignal.ts +0 -20
- package/src/useSignalValue.ts +0 -36
- package/tsconfig.json +0 -7
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @hoosk/minisignals-react
|
|
2
|
+
|
|
3
|
+
The React 18 adapter for `@hoosk/minisignals`.
|
|
4
|
+
|
|
5
|
+
This package provides custom hooks to seamlessly integrate the agnostic reactive signals with React's component lifecycle and rendering engine. It utilizes React 18's `useSyncExternalStore` for tear-free concurrent rendering.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @hoosk/minisignals @hoosk/minisignals-react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Basic Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { useSignal, useSignalValue } from '@hoosk/minisignals-react';
|
|
17
|
+
|
|
18
|
+
function Counter() {
|
|
19
|
+
// Create a local signal that survives re-renders
|
|
20
|
+
const count = useSignal(0);
|
|
21
|
+
|
|
22
|
+
// Connect the signal to trigger React renders on mutation
|
|
23
|
+
const value = useSignalValue(count);
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div>
|
|
27
|
+
<p>Count: {value}</p>
|
|
28
|
+
<button onClick={() => count.value++}>Increment</button>
|
|
29
|
+
</div>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
For full documentation, please refer to the [main repository README](../../README.md).
|
package/package.json
CHANGED
package/src/index.ts
DELETED
package/src/useSignal.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
2
|
-
import { signal, Signal } from '@hoosk/minisignals';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a signal that is tied to the component's lifecycle.
|
|
6
|
-
* The signal instance is preserved across re-renders.
|
|
7
|
-
* NOTE: To trigger a re-render on value change, you must use `useSignalValue(sig)`.
|
|
8
|
-
*
|
|
9
|
-
* @param initialValue The starting value of the signal.
|
|
10
|
-
* @returns A stable Signal instance.
|
|
11
|
-
*/
|
|
12
|
-
export function useSignal<T>(initialValue: T): Signal<T> {
|
|
13
|
-
const signalRef = useRef<Signal<T> | null>(null);
|
|
14
|
-
|
|
15
|
-
if (signalRef.current === null) {
|
|
16
|
-
signalRef.current = signal(initialValue);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return signalRef.current;
|
|
20
|
-
}
|
package/src/useSignalValue.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { useSyncExternalStore, useCallback } from 'react';
|
|
2
|
-
import { effect, Signal, Computed } from '@hoosk/minisignals';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A hook that subscribes to a signal or computed value and triggers
|
|
6
|
-
* a React re-render whenever the value changes.
|
|
7
|
-
* Uses React 18's useSyncExternalStore for tear-free concurrent rendering.
|
|
8
|
-
*
|
|
9
|
-
* @param sig The Signal or Computed to observe.
|
|
10
|
-
* @returns The current value of the signal.
|
|
11
|
-
*/
|
|
12
|
-
export function useSignalValue<T>(sig: Signal<T> | Computed<T>): T {
|
|
13
|
-
const subscribe = useCallback(
|
|
14
|
-
(onStoreChange: () => void) => {
|
|
15
|
-
let isInitialRun = true;
|
|
16
|
-
|
|
17
|
-
const unsubscribe = effect(() => {
|
|
18
|
-
const _val = sig.value;
|
|
19
|
-
|
|
20
|
-
if (!isInitialRun) {
|
|
21
|
-
onStoreChange();
|
|
22
|
-
}
|
|
23
|
-
isInitialRun = false;
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
return unsubscribe;
|
|
27
|
-
},
|
|
28
|
-
[sig]
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
return useSyncExternalStore(
|
|
32
|
-
subscribe,
|
|
33
|
-
() => sig.value, // getSnapshot
|
|
34
|
-
() => sig.value // getServerSnapshot (SSR support)
|
|
35
|
-
);
|
|
36
|
-
}
|