|
| 1 | +# hook-store |
| 2 | + |
| 3 | +## Use |
| 4 | + |
| 5 | +### install |
| 6 | + |
| 7 | +`npm install hooxjs -S` |
| 8 | + |
| 9 | +### create some store |
| 10 | + |
| 11 | +```javascript |
| 12 | +// counterStore.js |
| 13 | +import hoox from 'hooxjs' |
| 14 | + |
| 15 | +const state = { |
| 16 | + count: 1 |
| 17 | +} |
| 18 | + |
| 19 | +export const { getHooxState, useHooxState, createContainer } = hoox(state) |
| 20 | + |
| 21 | +// some action |
| 22 | +export const up = () => { |
| 23 | + const [hooxState, setHooxState] = getHooxState() |
| 24 | + return setHooxState({ |
| 25 | + count: hooxState.count + 1 |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +// some computed state |
| 30 | +export const useDoubleCount = () => { |
| 31 | + const [hooxState] = useHooxState() |
| 32 | + return hooxState.count * 2 |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### use store |
| 37 | + |
| 38 | +```javascript |
| 39 | +import React from 'react' |
| 40 | +import ReactDom from 'react-dom' |
| 41 | +import { useHooxState, useDoubleCount, up } from './counterStore' |
| 42 | + |
| 43 | +function Child() { |
| 44 | + const doubleCount = useDoubleCount() |
| 45 | + return <div>{doubleCount}</div> |
| 46 | +} |
| 47 | + |
| 48 | +function Counter() { |
| 49 | + const [hooxState] = useHooxState() |
| 50 | + return ( |
| 51 | + <div> |
| 52 | + <div>{hooxState.count}</div> |
| 53 | + <div onClick={() => up()} /> |
| 54 | + <Child /> |
| 55 | + </div> |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +const Container = createContainer(Counter) |
| 60 | + |
| 61 | +ReactDom.render(<Container />, document.getElementById('#root')) |
| 62 | +``` |
| 63 | + |
| 64 | +## API |
| 65 | + |
| 66 | +### hoox(state) |
| 67 | + |
| 68 | +```javascript |
| 69 | +const state = { count: 0 } |
| 70 | + |
| 71 | +export const { |
| 72 | + Provider, |
| 73 | + getHooxState, |
| 74 | + useHooxState, |
| 75 | + getResetState, |
| 76 | + createContainer |
| 77 | +} = hoox(state) |
| 78 | +``` |
| 79 | + |
| 80 | +### Provider |
| 81 | + |
| 82 | +hooxState will combine the initialState of Provider props |
| 83 | + |
| 84 | +```javascript |
| 85 | +function App() { |
| 86 | + return <Provider initialState={{ count: 1 }}> |
| 87 | + <YourFunctionComponent> |
| 88 | + </Provider> |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### createContainer |
| 93 | + |
| 94 | +suger of Provider |
| 95 | + |
| 96 | +hooxState will combine the initialState of createContainer args[1] |
| 97 | + |
| 98 | +```javascript |
| 99 | +const App = createContainer(YourFunctionComponent, { count: 2 }) |
| 100 | +``` |
| 101 | + |
| 102 | +### useHooxState |
| 103 | + |
| 104 | +using this api, build your hook |
| 105 | + |
| 106 | +```javascript |
| 107 | +export const useDoubleCount = () => { |
| 108 | + const [hooxState, setHookSate, resetHooxState] = useHooxState() |
| 109 | + const { count } = hooxState |
| 110 | + return [count * 2, () => setHookSate({ count: count * 2 })] |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### getHooxState |
| 115 | + |
| 116 | +using this api, build your action |
| 117 | + |
| 118 | +```javascript |
| 119 | +export const up = () => { |
| 120 | + const [hooxState, setHooxState, resetHooxState] = getHooxState() |
| 121 | + return setHooxState({ |
| 122 | + count: hooxState.count + 1 |
| 123 | + }) |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### setHooxState |
| 128 | + |
| 129 | +it behaves like `setState` of class Components, but no callback |
| 130 | + |
| 131 | +```javascript |
| 132 | +// update State |
| 133 | +export const updateWithRecordOld = newCount => { |
| 134 | + const [oldState, setHooxState] = getHooxState() |
| 135 | + return setHooxState({ |
| 136 | + count: newCount, |
| 137 | + oldCount: oldState.count |
| 138 | + }) |
| 139 | +} |
| 140 | + |
| 141 | +// aonther way to use oldState |
| 142 | +export const up = (key, value) => { |
| 143 | + const [, setHooxState] = getHooxState() |
| 144 | + return setHooxState(oldState => ({ |
| 145 | + count: oldState.count + 1 |
| 146 | + })) |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### resetHooxState |
| 151 | + |
| 152 | +it behaves like `setState` of `useState` hook |
| 153 | + |
| 154 | +```javascript |
| 155 | +// get resetHooxState from getHooxState or useHooxState |
| 156 | +export const reset = (key, value) => { |
| 157 | + const [, , resetHooxState] = getHooxState() |
| 158 | + return resetHooxState({ count: 0 }) |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +```javascript |
| 163 | +// get resetHooxState from getResetState |
| 164 | +export const reset = (key, value) => { |
| 165 | + const resetHooxState = getResetState() |
| 166 | + return resetHooxState({ count: 0 }) |
| 167 | +} |
| 168 | +``` |
0 commit comments