17 |
18 | ## Overview
19 | 🐻 Clean-State is a neat, compact state management tool. It drops all of React's historical baggage, uses native hooks to implement it, and gets rid of Redux's problem of invalid rendering during status updates. At the architectural level it is automatically organized through a very simple API. 🍋 If you're not building an aircraft carrier and you're tired of having a large, complex and unwield-of-use State management library, try clean-state. It is small and exquisite, the performance of the extreme can meet your needs.
20 |
21 | ## Features
22 | 1. Using native hooks implementation, zero external dependencies.
23 | 2. The structure is simple, the module layer granularity is fine and measurable, and the division is clear.
24 | 3. Excellent performance, can do module level accurate update.
25 | 4. Native support side effects.
26 | 5. It's extremely small, just 200 lines of code.
27 | 6. Just React syntax, zero learning access cost.
28 | 7. TypeScript friendly and automatically deduces module types.
29 | 8. Support for Redux - Tool debugging tool.
30 | 9. Perfect support for RN.
31 |
32 | ## Installation
33 | ```javascript
34 | npm i clean-state --save
35 | ```
36 |
37 | ## Usage
38 | #### 1.Define a module
39 | ```javascript
40 | // modules/user.ts
41 | const state = {
42 | name: 'test'
43 | }
44 |
45 | const user = {
46 | state,
47 | reducers: {
48 | setName({payload, state}) {
49 | return {...state, ...payload}
50 | }
51 | },
52 | effects: {
53 | async fetchNameAndSet({dispatch}) {
54 | const name = await Promise.resolve('fetch_name')
55 | dispatch.user.setName({name})
56 | }
57 | }
58 | }
59 |
60 | export default user;
61 | ```
62 |
63 | #### 2.Registration module
64 | ```javascript
65 | // modules/index.ts
66 | import user from './user'
67 | import bootstrap from 'clean-state'
68 |
69 | const modules = { user }
70 | export const {useModule, dispatch} = bootstrap(modules);
71 | ```
72 |
73 | #### 3.Use the module
74 | ```javascript
75 | // page.ts
76 | import {useCallback} from 'react'
77 | import { useModule, dispatch } from './modules'
78 |
79 | function App() {
80 | /**
81 | * Here you can also pass in an array and return multiple module states at the same time
82 | * const {user, project} = useModule(['user', 'project'])
83 | */
84 | const { user } = useModule('user')
85 | const onChange = useCallback((e)=> {
86 | const { target } = e
87 | dispatch.user.setName({name: target.value})
88 | }, [])
89 |
90 | const onClick = useCallback(()=> {
91 | dispatch.user.fetchNameAndSet()
92 | }, [])
93 |
94 | return (
95 |
96 |
97 |
98 | name: {user.name}
99 |
100 |
101 | modify:
102 |
103 |
104 |
105 |
106 | );
107 | }
108 |
109 | export default App;
110 | ```
111 |
112 | ## Mixin
113 |
114 | In many cases, there are common states, reducers, or effects between multiple modules, and here we expose the methods to prevent users from making duplicate declarations in each module.
115 |
116 | ```javascript
117 | // common.ts
118 | const common = {
119 | reducers: {
120 | setValue({payload, state}: {payload: Record, state: State}): State {
121 | return {...state, ...payload}
122 | }
123 | }
124 | }
125 | export default common;
126 |
127 | // modules/index.ts
128 | import commont from './common'
129 | import user from './user'
130 | import { mixin } from 'clean-state';
131 |
132 | // Mix Common's setValue method into the User module
133 | const modules = mixin(common, { user })
134 |
135 | // You can now call the dispatch.user.setValue method on other pages
136 | export const {useModule, dispatch} = bootstrap(modules);
137 |
138 | ```
139 |
140 | ## Module
141 | ### `state`
142 | Module state, which is a property object.
143 | ```
144 | {
145 | name: 'zhangsan',
146 | order: 1
147 | }
148 | ```
149 | ### `reducers`
150 | A collection of handlers that change the state of a module, returning the latest state.
151 | ```
152 | {
153 | setValue({payload, state, rootState}) {
154 | return {...state, ...payload}
155 | }
156 | }
157 | ```
158 |
159 | ### `effects`
160 | Module's collection of side effects methods that handle asynchronous calls.
161 | ```
162 | {
163 | async fetchAndSetValue({payload, state, rootState, dispatch}) {
164 | const newOrder = await fetch('xxx')
165 | dispatch.user.setValue({order: newOrder})
166 | }
167 | }
168 | ```
169 |
170 | ## API
171 |
172 | ### `bootstrap(modules)`
173 | | Property | Description | Type |
174 | | :----: | :----: | :----: |
175 | | modules | A collection of registered modules | {string, Module} |
176 |
177 | ### `useModule(moduleName)`
178 | | Property | Description | Type |
179 | | :----: | :----: | :----: |
180 | | moduleName | The name of the module used returns the corresponding status | string / string[] |
181 |
182 | ### `mixin(common, modules)`
183 | | Property | Description | Type |
184 | | :----: | :----: | :----: |
185 | | common | Public modules that need to be injected | Module |
186 | | modules | A collection of registered modules | Module |
187 |
188 | ### `dispatch.{moduleName}.{fnName}(payload)`
189 | | Property | Description | Type |
190 | | :----: | :----: | :----: |
191 | | moduleName | The specific module name of the call should be registered in Bootstrap | string |
192 | | fnName | The method name of the call module, reducer/effect | string |
193 | | payload | The load value passed | object |
194 |
195 | ## Debugging
196 | You can use [cs-redux-devtool](https://github.com/freezeYe/cs-redux-devtool) to debug your project and track historical data changes.
197 |
198 |
199 |
200 |
201 | ## Notice
202 |
203 | Dispatch calls take precedence at effects-> reducers, so when there are reducers and effects with the same name under a module, only effects are executed.
204 |
205 | ## Issues
206 |
207 | If you have better suggestions for this library, or have any problems using it, you can write them here: [https://github.com/tnfe/clean-state/issues](https://github.com/tnfe/clean-state/issues)
208 |
209 | ## License
210 | [MIT](./LICENSE)
211 |
--------------------------------------------------------------------------------