@@ -27,15 +27,34 @@ npm install react-modelx
2727
2828react-model keep the state and actions in a global store. So you need to register them before using.
2929
30+ ` index.ts `
31+
3032``` typescript
3133import { registerModel } from ' react-modelx'
3234import Home from ' ../model/home.model'
3335import Shared from ' ../model/shared.model'
3436
35- registerModel ({
36- Home ,
37- Shared
38- })
37+ const models = { Home , Shared }
38+
39+ export const {
40+ useStore
41+ }: { useStore: UseStore <keyof typeof models , typeof models > } = registerModel (
42+ models
43+ )
44+ ```
45+
46+ ` index.js `
47+
48+ ``` javascript
49+ import { registerModel } from ' react-modelx'
50+ import Home from ' ../model/home.model'
51+ import Shared from ' ../model/shared.model'
52+
53+ const models = { Home, Shared }
54+
55+ export const {
56+ useStore
57+ }
3958```
4059
4160### useStore
@@ -44,7 +63,7 @@ The functional component in React 16.7 can use Hooks to connect the global store
4463
4564``` javascript
4665import React from ' react'
47- import { useStore } from ' react-modelx '
66+ import { useStore } from ' ./index '
4867
4968export default () => {
5069 const [state , actions ] = useStore (' Home' )
@@ -65,6 +84,69 @@ export default () => {
6584}
6685```
6786
87+ ### Model
88+
89+ Every model have their own state and actions.
90+
91+ ``` typescript
92+ const initialState = {
93+ counter: 0 ,
94+ light: false ,
95+ response: {} as {
96+ code: number
97+ message: string
98+ }
99+ }
100+
101+ type StateType = typeof initialState
102+ type ActionsParamType = {
103+ increment: number
104+ openLight: undefined
105+ get: undefined
106+ } // You only need to tag the type of params here !
107+
108+ const Model = {
109+ actions: {
110+ increment : async (state , _ , params ) => {
111+ return {
112+ counter: state .counter + (params || 1 )
113+ }
114+ },
115+ openLight : async (state , actions ) => {
116+ await actions .increment (1 ) // You can use other actions within the model
117+ await actions .get () // support async functions (block actions)
118+ actions .get ()
119+ await actions .increment (1 ) // + 1
120+ await actions .increment (1 ) // + 2
121+ await actions .increment (1 ) // + 3 as expected !
122+ return { light: ! state .light }
123+ },
124+ get : async () => {
125+ await new Promise ((resolve , reject ) =>
126+ setTimeout (() => {
127+ resolve ()
128+ }, 3000 )
129+ )
130+ return {
131+ response: {
132+ code: 200 ,
133+ message: ` ${new Date ().toLocaleString ()} open light success `
134+ }
135+ }
136+ }
137+ },
138+ state: initialState
139+ } as ModelType <StateType , ActionsParamType > // The Modal actions type will generate automatically by the StateType and ActionParamsType
140+
141+ export default Model
142+
143+ type ConsumerActionsType = getConsumerActionsType <typeof Model .actions >
144+ type ConsumerType = { actions: ConsumerActionsType ; state: StateType }
145+ type ActionType = ConsumerActionsType
146+
147+ export { ConsumerType , StateType , ActionType }
148+ ```
149+
68150### getState
69151
70152Key Point: [ State variable not updating in useEffect callback] ( https://github.com/facebook/react/issues/14066 )
@@ -92,6 +174,8 @@ const BasicHook = () => {
92174}
93175```
94176
177+ ## Other Concept required by Class Component
178+
95179### Provider
96180
97181The global state standalone can not effect the react class components, we need to provide the state to react root component.
@@ -182,66 +266,3 @@ export default connect(
182266 mapProps
183267)(TSCounter )
184268```
185-
186- ### Model
187-
188- Every model have their own state and actions.
189-
190- ``` typescript
191- const initialState = {
192- counter: 0 ,
193- light: false ,
194- response: {} as {
195- code: number
196- message: string
197- }
198- }
199-
200- type StateType = typeof initialState
201- type ActionsParamType = {
202- increment: number
203- openLight: undefined
204- get: undefined
205- } // You only need to tag the type of params here !
206-
207- const Model = {
208- actions: {
209- increment : async (state , _ , params ) => {
210- return {
211- counter: state .counter + (params || 1 )
212- }
213- },
214- openLight : async (state , actions ) => {
215- await actions .increment (1 ) // You can use other actions within the model
216- await actions .get () // support async functions (block actions)
217- actions .get ()
218- await actions .increment (1 ) // + 1
219- await actions .increment (1 ) // + 2
220- await actions .increment (1 ) // + 3 as expected !
221- return { light: ! state .light }
222- },
223- get : async () => {
224- await new Promise ((resolve , reject ) =>
225- setTimeout (() => {
226- resolve ()
227- }, 3000 )
228- )
229- return {
230- response: {
231- code: 200 ,
232- message: ` ${new Date ().toLocaleString ()} open light success `
233- }
234- }
235- }
236- },
237- state: initialState
238- } as ModelType <StateType , ActionsParamType > // The Modal actions type will generate automatically by the StateType and ActionParamsType
239-
240- export default Model
241-
242- type ConsumerActionsType = getConsumerActionsType <typeof Model .actions >
243- type ConsumerType = { actions: ConsumerActionsType ; state: StateType }
244- type ActionType = ConsumerActionsType
245-
246- export { ConsumerType , StateType , ActionType }
247- ```
0 commit comments