react-better-model 0.1.1 → 0.1.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/lib/index.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare class Model<S, E = {}> {
17
17
  dispatch: <K extends keyof E>(key: K, data?: (E[K] extends undefined ? never : E[K]) | undefined) => void;
18
18
  reset: () => void;
19
19
  }
20
+ export declare function useModelCtx<M extends Model<M['state'], M['events']>>(ctx: Context<M>): M;
20
21
  export declare function useModelInstanceState<K extends keyof M['state'], M extends Model<M['state'], M['events']>>(viewModel: M, key: K): [M['state'][K], (data: M['state'][K]) => void];
21
22
  export declare function useModelCtxState<K extends keyof M['state'], M extends Model<M['state'], M['events']>>(ctx: Context<M>, key: K): [M["state"][K], (data: M["state"][K]) => void];
22
23
  export declare function useModelInstanceEvent<K extends keyof M['events'], D extends M['events'], M extends Model<M['state'], M['events']>>(viewModel: M, ns: K, cb?: Sub<D, K>): (data?: (D[K] extends undefined ? never : D[K]) | undefined) => void;
package/lib/index.js CHANGED
@@ -11,7 +11,7 @@ var __assign = (this && this.__assign) || function () {
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.useModelCtxEvent = exports.useModelInstanceEvent = exports.useModelCtxState = exports.useModelInstanceState = exports.Model = void 0;
14
+ exports.useModelCtxEvent = exports.useModelInstanceEvent = exports.useModelCtxState = exports.useModelInstanceState = exports.useModelCtx = exports.Model = void 0;
15
15
  var react_1 = require("react");
16
16
  var Model = /** @class */ (function () {
17
17
  function Model(state, events) {
@@ -86,6 +86,10 @@ var Model = /** @class */ (function () {
86
86
  return Model;
87
87
  }());
88
88
  exports.Model = Model;
89
+ function useModelCtx(ctx) {
90
+ return (0, react_1.useContext)(ctx);
91
+ }
92
+ exports.useModelCtx = useModelCtx;
89
93
  function useModelInstanceState(viewModel, key) {
90
94
  var _a = (0, react_1.useState)(viewModel.state[key]), value = _a[0], setValue = _a[1];
91
95
  (0, react_1.useEffect)(function () { return viewModel.onStateChange(key, setValue); }, []);
package/package.json CHANGED
@@ -1,9 +1,16 @@
1
1
  {
2
2
  "name": "react-better-model",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
+ "keywords": [
8
+ "React",
9
+ "Shared",
10
+ "Model",
11
+ "Store",
12
+ "Events"
13
+ ],
7
14
  "repository": {
8
15
  "type": "git",
9
16
  "url": "git@github.com:ingvardm/react-model-hooks.git"
package/readme.md CHANGED
@@ -0,0 +1,101 @@
1
+ React Better Model
2
+ ===
3
+ Easy way to share state and events between components and services.
4
+
5
+ Installation:
6
+ ===
7
+ ```bash
8
+ npm i react-better-model
9
+ ```
10
+ or
11
+ ```bash
12
+ yarn add react-better-model
13
+ ```
14
+
15
+ Usage
16
+ ===
17
+ ```typescript
18
+ // CounterModel.ts
19
+ export const initialState = {
20
+ count: 0,
21
+ }
22
+
23
+ export const defaultEventData = {
24
+ click: undefined,
25
+ }
26
+
27
+ // create context
28
+ export const CounterModelCtx = createContext(new Model(initialState, defaultEventData))
29
+
30
+ ```
31
+
32
+ ```typescript
33
+ // CounterText.tsx
34
+ const CounterText = () => {
35
+ const [count, setCount] = useModelCtxState(CounterModelCtx, 'count')
36
+
37
+ return <p>{count}</p>
38
+ }
39
+ ```
40
+
41
+ ```typescript
42
+ // CounterIncrementButton.tsx
43
+ const CounterIncrementButton = () => {
44
+ const [count, setCount] = useModelCtxState(CounterModelCtx, 'count')
45
+
46
+ const onClick = useCallback(() => {
47
+ setCount(count + 1)
48
+ }, [count])
49
+
50
+ return <button onClick={onClick}>Press me!</button>
51
+ }
52
+ ```
53
+
54
+ ```typescript
55
+ // CounterClearButton.tsx
56
+ const CounterClearButton = () => {
57
+ const dispatchClear = useModelCtxEvent(CounterModelCtx, 'clear')
58
+
59
+ const onClick = useCallback(() => {
60
+ dispatchClear()
61
+ }, [])
62
+
63
+ return <button onClick={onClick}>Clear</button>
64
+ }
65
+ ```
66
+
67
+ ```typescript
68
+ // Counter.tsx
69
+ const Counter = () => {
70
+ const model = useMemo(() => {
71
+ return new Model(initialState, defaultEventData)
72
+ }, [])
73
+
74
+ const [count, setCount] = useModelInstanceState(model, 'count')
75
+
76
+ const onClear = useCallback(() => {
77
+ setCount(0)
78
+ }, [])
79
+
80
+ useModelInstanceEvent(model, 'clear', onClear)
81
+
82
+ return <div>
83
+ <CounterModelCtx.Provider value={model}>
84
+ <CounterText />
85
+ <CounterIncrementButton />
86
+ <CounterClearButton />
87
+ </CounterModelCtx.Provider>
88
+ </div>
89
+ }
90
+ ```
91
+
92
+ ```typescript
93
+ // app.js
94
+ const App = () => {
95
+ return <div>
96
+ <Counter />
97
+ <Counter />
98
+ <Counter />
99
+ </div>
100
+ }
101
+ ```