@view-models/react 1.0.0 → 1.1.0
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/dist/useModelState.d.ts +29 -0
- package/dist/useModelState.d.ts.map +1 -1
- package/dist/useModelState.js +29 -0
- package/package.json +1 -1
- package/src/useModelState.ts +29 -0
package/dist/useModelState.d.ts
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
1
|
import type { ViewModel, State } from "@view-models/core";
|
|
2
|
+
/**
|
|
3
|
+
* A React hook that subscribes a component to a ViewModel's state updates.
|
|
4
|
+
*
|
|
5
|
+
* This hook connects a React component to a ViewModel instance, automatically
|
|
6
|
+
* subscribing to state changes and triggering re-renders when the state updates.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The state type, which must extend the State interface
|
|
9
|
+
* @param model - The ViewModel instance to subscribe to
|
|
10
|
+
* @returns The current state from the ViewModel
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* class CounterViewModel extends ViewModel<{ count: number }> {
|
|
15
|
+
* increment = () => this.update(({ count }) => ({ count: count + 1 }));
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* function Counter() {
|
|
19
|
+
* const counterModel = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
20
|
+
* const { count } = useModelState(counterModel);
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <div>
|
|
24
|
+
* <p>Count: {count}</p>
|
|
25
|
+
* <button onClick={counterModel.increment}>+</button>
|
|
26
|
+
* </div>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
2
31
|
export declare function useModelState<T extends State>(model: ViewModel<T>): T;
|
|
3
32
|
//# sourceMappingURL=useModelState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useModelState.d.ts","sourceRoot":"","sources":["../src/useModelState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1D,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAErE"}
|
|
1
|
+
{"version":3,"file":"useModelState.d.ts","sourceRoot":"","sources":["../src/useModelState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAErE"}
|
package/dist/useModelState.js
CHANGED
|
@@ -1,4 +1,33 @@
|
|
|
1
1
|
import { useSyncExternalStore } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* A React hook that subscribes a component to a ViewModel's state updates.
|
|
4
|
+
*
|
|
5
|
+
* This hook connects a React component to a ViewModel instance, automatically
|
|
6
|
+
* subscribing to state changes and triggering re-renders when the state updates.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The state type, which must extend the State interface
|
|
9
|
+
* @param model - The ViewModel instance to subscribe to
|
|
10
|
+
* @returns The current state from the ViewModel
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* class CounterViewModel extends ViewModel<{ count: number }> {
|
|
15
|
+
* increment = () => this.update(({ count }) => ({ count: count + 1 }));
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* function Counter() {
|
|
19
|
+
* const counterModel = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
20
|
+
* const { count } = useModelState(counterModel);
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <div>
|
|
24
|
+
* <p>Count: {count}</p>
|
|
25
|
+
* <button onClick={counterModel.increment}>+</button>
|
|
26
|
+
* </div>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
2
31
|
export function useModelState(model) {
|
|
3
32
|
return useSyncExternalStore(model.subscribe.bind(model), () => model.state);
|
|
4
33
|
}
|
package/package.json
CHANGED
package/src/useModelState.ts
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
import { useSyncExternalStore } from "react";
|
|
2
2
|
import type { ViewModel, State } from "@view-models/core";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* A React hook that subscribes a component to a ViewModel's state updates.
|
|
6
|
+
*
|
|
7
|
+
* This hook connects a React component to a ViewModel instance, automatically
|
|
8
|
+
* subscribing to state changes and triggering re-renders when the state updates.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The state type, which must extend the State interface
|
|
11
|
+
* @param model - The ViewModel instance to subscribe to
|
|
12
|
+
* @returns The current state from the ViewModel
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* class CounterViewModel extends ViewModel<{ count: number }> {
|
|
17
|
+
* increment = () => this.update(({ count }) => ({ count: count + 1 }));
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* function Counter() {
|
|
21
|
+
* const counterModel = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
22
|
+
* const { count } = useModelState(counterModel);
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <div>
|
|
26
|
+
* <p>Count: {count}</p>
|
|
27
|
+
* <button onClick={counterModel.increment}>+</button>
|
|
28
|
+
* </div>
|
|
29
|
+
* );
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
4
33
|
export function useModelState<T extends State>(model: ViewModel<T>): T {
|
|
5
34
|
return useSyncExternalStore(model.subscribe.bind(model), () => model.state);
|
|
6
35
|
}
|