@view-models/react 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sune Simonsen <sune@we-knowhow.dk>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,132 @@
1
+ # @view-models/react
2
+
3
+ React integration for [@view-models/core](https://github.com/sunesimonsen/view-models-core).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @view-models/react @view-models/core
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ The `useModelState` hook allows you to subscribe to state updates from a `ViewModel` instance.
14
+
15
+ ```tsx
16
+ import { ViewModel } from "@view-models/core";
17
+ import { useModelState } from "@view-models/react";
18
+
19
+ type CounterState = Readonly<{ count: number }>;
20
+
21
+ class CounterViewModel extends ViewModel<CounterState> {
22
+ increment = () => {
23
+ this.update(({ count }) => ({ count: count + 1 }));
24
+ };
25
+
26
+ decrement = () => {
27
+ this.update(({ count }) => ({ count: count - 1 }));
28
+ };
29
+ }
30
+
31
+ const counterModel = new CounterViewModel({ count: 0 });
32
+
33
+ function Counter() {
34
+ const { count } = useModelState(counterModel);
35
+
36
+ return (
37
+ <div>
38
+ <p>Count: {count}</p>
39
+ <button onClick={counterModel.increment}>+</button>
40
+ <button onClick={counterModel.decrement}>-</button>
41
+ </div>
42
+ );
43
+ }
44
+ ```
45
+
46
+ ### Creating view models inside components
47
+
48
+ When you need to create a view model from within a React component, use `useMemo` to ensure the model is only created once.
49
+
50
+ It's good practice to provide both a component that creates its own model and a customizable version that accepts a model as a prop:
51
+
52
+ ```tsx
53
+ import { useMemo } from "react";
54
+ import { ViewModel } from "@view-models/core";
55
+ import { useModelState } from "@view-models/react";
56
+
57
+ type CounterState = Readonly<{ count: number }>;
58
+
59
+ class CounterViewModel extends ViewModel<CounterState> {
60
+ increment = () => {
61
+ this.update(({ count }) => ({ count: count + 1 }));
62
+ };
63
+
64
+ decrement = () => {
65
+ this.update(({ count }) => ({ count: count - 1 }));
66
+ };
67
+ }
68
+
69
+ // Customizable version that accepts a model
70
+ function CustomCounter({ model }: { model: CounterViewModel }) {
71
+ const { count } = useModelState(model);
72
+
73
+ return (
74
+ <div>
75
+ <p>Count: {count}</p>
76
+ <button onClick={model.increment}>+</button>
77
+ <button onClick={model.decrement}>-</button>
78
+ </div>
79
+ );
80
+ }
81
+
82
+ // Simple version that creates its own model
83
+ function Counter() {
84
+ const model = useMemo(() => new CounterViewModel({ count: 0 }), []);
85
+ return <CustomCounter model={model} />;
86
+ }
87
+ ```
88
+
89
+ ## API
90
+
91
+ ### `useModelState<T>(model: ViewModel<T>): T`
92
+
93
+ A React hook that subscribes to state updates from a ViewModel instance.
94
+
95
+ **Parameters:**
96
+
97
+ - `model`: A ViewModel instance to subscribe to
98
+
99
+ **Returns:**
100
+
101
+ - The current state of the ViewModel
102
+
103
+ **Features:**
104
+
105
+ - Automatically subscribes to state updates when the component mounts
106
+ - Automatically unsubscribes when the component unmounts
107
+ - Re-renders the component when the ViewModel state changes
108
+ - Multiple components can subscribe to the same ViewModel
109
+
110
+ ## License
111
+
112
+ MIT License
113
+
114
+ Copyright (c) 2026 Sune Simonsen <sune@we-knowhow.dk>
115
+
116
+ Permission is hereby granted, free of charge, to any person obtaining a copy
117
+ of this software and associated documentation files (the "Software"), to deal
118
+ in the Software without restriction, including without limitation the rights
119
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
120
+ copies of the Software, and to permit persons to whom the Software is
121
+ furnished to do so, subject to the following conditions:
122
+
123
+ The above copyright notice and this permission notice shall be included in all
124
+ copies or substantial portions of the Software.
125
+
126
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
127
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
128
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
129
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
130
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
131
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
132
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export * from "./useModelState.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./useModelState.js";
@@ -0,0 +1,3 @@
1
+ import type { ViewModel, State } from "@view-models/core";
2
+ export declare function useModelState<T extends State>(model: ViewModel<T>): T;
3
+ //# sourceMappingURL=useModelState.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,4 @@
1
+ import { useSyncExternalStore } from "react";
2
+ export function useModelState(model) {
3
+ return useSyncExternalStore(model.subscribe.bind(model), () => model.state);
4
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@view-models/react",
3
+ "version": "1.0.0",
4
+ "description": "React integration for @view-models/core",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "src"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "typecheck": "tsc --noEmit",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest",
23
+ "test:coverage": "vitest run --coverage",
24
+ "format": "prettier --write .",
25
+ "format:check": "prettier --check .",
26
+ "prepublishOnly": "npm run build"
27
+ },
28
+ "keywords": [
29
+ "react",
30
+ "hooks",
31
+ "view-models",
32
+ "state-management"
33
+ ],
34
+ "author": "Sune Simonsen",
35
+ "license": "MIT",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/sunesimonsen/view-models-react.git"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/sunesimonsen/view-models-react/issues"
42
+ },
43
+ "homepage": "https://github.com/sunesimonsen/view-models-react#readme",
44
+ "engines": {
45
+ "node": ">=18.0.0"
46
+ },
47
+ "peerDependencies": {
48
+ "@view-models/core": "^1.1.0",
49
+ "react": ">=17.0.0"
50
+ },
51
+ "devDependencies": {
52
+ "@testing-library/react": "^16.1.0",
53
+ "@types/react": "^18.3.18",
54
+ "@view-models/core": "^1.1.0",
55
+ "@vitest/coverage-v8": "^2.1.8",
56
+ "jsdom": "^26.0.0",
57
+ "prettier": "^3.4.2",
58
+ "react": "^18.3.1",
59
+ "react-dom": "^18.3.1",
60
+ "typescript": "^5.7.3",
61
+ "vitest": "^2.1.8"
62
+ },
63
+ "publishConfig": {
64
+ "access": "public"
65
+ }
66
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./useModelState.js";
@@ -0,0 +1,6 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import type { ViewModel, State } from "@view-models/core";
3
+
4
+ export function useModelState<T extends State>(model: ViewModel<T>): T {
5
+ return useSyncExternalStore(model.subscribe.bind(model), () => model.state);
6
+ }