react-better-model 0.1.1 → 0.1.2
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/package.json +1 -1
- package/readme.md +101 -0
package/package.json
CHANGED
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
|
+
```
|