|
| 1 | +# Hoox |
| 2 | + |
| 3 | +[English](./README.md) | 简体中文 |
| 4 | + |
| 5 | +[](https://travis-ci.org/wuomzfx/hoox) |
| 6 | +[](https://codecov.io/gh/wuomzfx/hoox) |
| 7 | +[](https://www.npmjs.com/package/hooxjs) |
| 8 | +[](https://www.npmjs.com/package/hooxjs) |
| 9 | + |
| 10 | +## 使用 |
| 11 | + |
| 12 | +### 安装 |
| 13 | + |
| 14 | +```javascript |
| 15 | +npm install hooxjs -S |
| 16 | +``` |
| 17 | + |
| 18 | +### 创建一个 Store |
| 19 | + |
| 20 | +```javascript |
| 21 | +// counterStore.js |
| 22 | +import createHoox from 'hooxjs' |
| 23 | + |
| 24 | +const state = { |
| 25 | + count: 1 |
| 26 | +} |
| 27 | + |
| 28 | +export const { getHoox, useHoox, createContainer } = createHoox(state) |
| 29 | + |
| 30 | +// 创建一个action |
| 31 | +export const up = () => { |
| 32 | + const [hooxState, setHoox] = getHoox() |
| 33 | + return setHoox({ |
| 34 | + count: hooxState.count + 1 |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +// 创建一个computed数据 |
| 39 | +export const useDoubleCount = () => { |
| 40 | + const [hooxState] = useHoox() |
| 41 | + return hooxState.count * 2 |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### 使用 Store |
| 46 | + |
| 47 | +```javascript |
| 48 | +import React from 'react' |
| 49 | +import ReactDom from 'react-dom' |
| 50 | +import { useHoox, useDoubleCount, up } from './counterStore' |
| 51 | + |
| 52 | +function Child() { |
| 53 | + const doubleCount = useDoubleCount() |
| 54 | + return <div>{doubleCount}</div> |
| 55 | +} |
| 56 | + |
| 57 | +function Counter() { |
| 58 | + const [hooxState] = useHoox() |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <div>{hooxState.count}</div> |
| 62 | + <div onClick={() => up()} /> |
| 63 | + <Child /> |
| 64 | + </div> |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +const Container = createContainer(Counter) |
| 69 | + |
| 70 | +ReactDom.render(<Container />, document.getElementById('#root')) |
| 71 | +``` |
| 72 | + |
| 73 | +## API |
| 74 | + |
| 75 | +### createHoox |
| 76 | + |
| 77 | +通过本`api`,初始化全局状态。 |
| 78 | + |
| 79 | +```javascript |
| 80 | +import createHoox from 'hooxjs' |
| 81 | + |
| 82 | +const state = { count: 0 } |
| 83 | + |
| 84 | +export const { |
| 85 | + Provider, |
| 86 | + getHoox, |
| 87 | + useHoox, |
| 88 | + setHoox, |
| 89 | + resetHoox, |
| 90 | + createContainer |
| 91 | +} = createHoox(state) |
| 92 | +``` |
| 93 | + |
| 94 | +### Provider |
| 95 | + |
| 96 | +由于 hoox 基于`context`实现,故而消费全局状态的组件需要包裹在相应`Provider`下。 |
| 97 | + |
| 98 | +`Provider`还提供了一个 Prop,`initialState`。它可选地接收一个对象,它会与`createHoox`时传递的初始状态合并,成为 hoox 的最终全局状态初始值。 |
| 99 | + |
| 100 | +```javascript |
| 101 | +function App() { |
| 102 | + return <Provider initialState={{ count: 1 }}> |
| 103 | + <YourFunctionComponent> |
| 104 | + </Provider> |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### createContainer |
| 109 | + |
| 110 | +这是一个`Provider`的语法糖。 |
| 111 | + |
| 112 | +`createContainer`的第一个参数是需要消费全局状态的函数式组件(只要根组件即可),第二个参数即同`Provider`的`initialState`,会与创建 `Store`时的状态合并成初始全局状态。 |
| 113 | + |
| 114 | +```javascript |
| 115 | +const App = createContainer(YourFunctionComponent, { count: 2 }) |
| 116 | +``` |
| 117 | + |
| 118 | +### useHoox |
| 119 | + |
| 120 | +本`api`主要用于函数式组件内直接消费/更新全局状态;或用于构建自定义全局 Hook。 |
| 121 | + |
| 122 | +**消费状态** |
| 123 | + |
| 124 | +```javascript |
| 125 | +function Counter() { |
| 126 | + const [hooxState] = useHoox() |
| 127 | + return ( |
| 128 | + <div> |
| 129 | + <div>{hooxState.count}</div> |
| 130 | + <div onClick={() => up()} /> |
| 131 | + <Child /> |
| 132 | + </div> |
| 133 | + ) |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +**构建自定义全局 Hook** |
| 138 | + |
| 139 | +```javascript |
| 140 | +export const useDoubleCount = () => { |
| 141 | + const [hooxState, setHoox, resetHoox] = useHoox() |
| 142 | + const { count } = hooxState |
| 143 | + return [count * 2, () => setHoox({ count: count * 2 })] |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### getHoox |
| 148 | + |
| 149 | +`getHoox`常用于创建一个全局`action/effect`。切忌,`getHoox`获取的全局状态不具有响应式。因此,如无特殊需要,不应该在函数式组件内直接引用。 |
| 150 | + |
| 151 | +**创建一个 action** |
| 152 | + |
| 153 | +```javascript |
| 154 | +export const up = () => { |
| 155 | + const [hooxState, setHoox, resetHoox] = getHoox() |
| 156 | + return setHoox({ |
| 157 | + count: hooxState.count + 1 |
| 158 | + }) |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +**如下使用,当全局状态变更时,`Counter`并不会重新渲染** |
| 163 | + |
| 164 | +```javascript |
| 165 | +function Counter() { |
| 166 | + const [hooxState] = getHoox() |
| 167 | + return ( |
| 168 | + <div> |
| 169 | + <div>{hooxState.count}</div> |
| 170 | + <div onClick={() => up()} /> |
| 171 | + <Child /> |
| 172 | + </div> |
| 173 | + ) |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### setHoox |
| 178 | + |
| 179 | +`setHoox`的行为跟类组件中的`setState`表现一致,会合并状态,但是没有回调。 |
| 180 | + |
| 181 | +`setHoox`可以直接从`createHoox(state)`的返回值中获取。 |
| 182 | + |
| 183 | +```javascript |
| 184 | +const { setHoox } = createHoox({ count: 0 }) |
| 185 | +export const updateCount = newCount => { |
| 186 | + return setHoox({ |
| 187 | + count: newCount |
| 188 | + }) |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +`setHoox` 也可以直接从`getHoox()`或 `useHoox()`的返回值中获取。 |
| 193 | + |
| 194 | +```javascript |
| 195 | +export const updateWithRecordOld = newCount => { |
| 196 | + const [oldState, setHoox] = getHoox() |
| 197 | + return setHoox({ |
| 198 | + count: newCount, |
| 199 | + oldCount: oldState.count |
| 200 | + }) |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +`setHoox`也支持传递一个函数,函数第一个入参为当前全局状态。 |
| 205 | + |
| 206 | +```javascript |
| 207 | +export const up = () => { |
| 208 | + const [, setHoox] = getHoox() |
| 209 | + return setHoox(oldState => ({ |
| 210 | + count: oldState.count + 1 |
| 211 | + })) |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +### resetHoox |
| 216 | + |
| 217 | +`resetHoox`的行为跟函数式组件中,`useState`返回的`setState`表现一致,它会重置全局状态。 |
| 218 | + |
| 219 | +`setHoox`可以直接从`createHoox(state)`的返回值中获取。 |
| 220 | + |
| 221 | +```javascript |
| 222 | +const { resetHoox } = createHoox({ count: 0 }) |
| 223 | +export const reset = () => { |
| 224 | + return resetHoox({ newCount: 0 }) |
| 225 | +} |
| 226 | +``` |
| 227 | + |
| 228 | +`resetHoox` 也可以直接从`getHoox()`或 `useHoox()`的返回值中获取。 |
| 229 | + |
| 230 | +```javascript |
| 231 | +export const reset = () => { |
| 232 | + const [, , resetHoox] = getHoox() |
| 233 | + return resetHoox({ newCount: 0 }) |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +### connect |
| 238 | + |
| 239 | +将全局状态注入到`React`组件的`props`之中。 |
| 240 | + |
| 241 | +#### 函数式组件 |
| 242 | + |
| 243 | +```javascript |
| 244 | +const { connect } = createHoox({ count: 0 }) |
| 245 | + |
| 246 | +const Counter = ({ count }) => { |
| 247 | + return <div>{count}</div> |
| 248 | +} |
| 249 | + |
| 250 | +export default connect(state => ({ count: state.count }))(Counter) |
| 251 | +``` |
| 252 | + |
| 253 | +#### 类组件 |
| 254 | + |
| 255 | +```jsx |
| 256 | +// jsx |
| 257 | +import React from 'react' |
| 258 | + |
| 259 | +const { connect } = createHoox({ count: 0 }) |
| 260 | + |
| 261 | +@connect(state => ({ count: state.count })) |
| 262 | +export default class Counter extends React.PureComponent { |
| 263 | + render() { |
| 264 | + return <div>{this.props.count}</div> |
| 265 | + } |
| 266 | +} |
| 267 | +``` |
| 268 | + |
| 269 | +PS: 由于装饰器不能修改被装饰对象的返回类型,`connect`的装饰器语法目前暂不支持`TypeScript`。 |
| 270 | + |
| 271 | +```tsx |
| 272 | +// tsx |
| 273 | +import React from 'react' |
| 274 | + |
| 275 | +const { connect } = createHoox({ count: 0 }) |
| 276 | + |
| 277 | +class Counter extends React.PureComponent<{ count: number }> { |
| 278 | + render() { |
| 279 | + return <div>{this.props.count}</div> |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +export default connect(state => ({ count: state.count }))(Counter) |
| 284 | +``` |
0 commit comments