|
| 1 | +--- |
| 2 | +slug: /ReduxStore |
| 3 | +title: Redux store |
| 4 | +--- |
| 5 | + |
| 6 | +🚧 It's a Work In Progress section 🚧 |
| 7 | + |
| 8 | +## Architecture |
| 9 | +The root file include configuration of redux. The two main constants are `reducers` and `persistConfig` |
| 10 | + |
| 11 | +```javascript |
| 12 | +const reducers = combineReducers({ |
| 13 | + startup, |
| 14 | + user, |
| 15 | +}) |
| 16 | + |
| 17 | +const persistConfig = { |
| 18 | + key: 'root', |
| 19 | + storage: AsyncStorage, |
| 20 | + whitelist: [], |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | + - `whitelist` includes state to persist (with `redux-persist`) |
| 25 | + - `reducers` includes all `reducer modules` |
| 26 | + |
| 27 | +## Slices |
| 28 | + |
| 29 | +A slice is a group of actions, states and reducers for a same module. For example, in this boilerplate, there are two slices : `Startup` and `User`. |
| 30 | +In each slice, an `index.js` file which combines each store's feature (`fetchOne.js` for the `User` slice example). |
| 31 | +We've decided to separate each feature in one file in order to avoid very large incomprehensible files. |
| 32 | +So each feature includes its scoped state, actions and related reducers. |
| 33 | + |
| 34 | +```javascript |
| 35 | +export default { |
| 36 | + initialState: { |
| 37 | + // Optionally, you can scope variables |
| 38 | + fetchOne: { loading: false, error: null }, |
| 39 | + }, |
| 40 | + action: buildAction('user/fetchOne', fetchOneUserService), |
| 41 | + reducers: buildReducers({ |
| 42 | + errorKey: 'fetchOne.error', // Optionally, if you scoped variables, you can use a key with dot notation |
| 43 | + loadingKey: 'fetchOne.loading', |
| 44 | + }), |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +In the `index.js` file, all features are merged in a slice where states, actions, and reducers are merged and placed in a slice. |
| 49 | + |
| 50 | +```javascript |
| 51 | +const moduleInitialState = { |
| 52 | + item: {}, |
| 53 | +} |
| 54 | + |
| 55 | +export default buildSlice('user', [FetchOne], moduleInitialState).reducer |
| 56 | +``` |
| 57 | + |
| 58 | +For the `User` example, the below state will be created : |
| 59 | +``` |
| 60 | +{ |
| 61 | + user: { |
| 62 | + item: {}, |
| 63 | + fetchOne: { |
| 64 | + loading: false, |
| 65 | + error: null, |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | +`Actions` will be : `user/fetchOne/pending`, `user/fetchOne/fulfilled`, `user/fetchOne/rejected`. |
| 71 | +For each action, a reducer is associated. |
| 72 | + |
| 73 | +## Redux-toolkit-wrapper |
| 74 | +The boilerplate includes a [wrapper of redux-toolkit](https://github.com/thecodingmachine/redux-toolkit-wrapper) to make it easier to use. It provides three helpers. |
| 75 | +If your are not familiar with redux-toolkit, please have a look at their [documentation](https://redux-toolkit.js.org/api/configureStore). |
| 76 | + |
| 77 | +### buildAction |
| 78 | +`buildAction` is a wrapper of [`createAsyncThunk`](https://redux-toolkit.js.org/api/createAsyncThunk). |
| 79 | + |
| 80 | +| Parameters | Description | Type | Default | |
| 81 | +| :-------------------- | :------------------------------------------ | :-------- | :--------- | |
| 82 | +| actionName | the name of the action | string | undefined | |
| 83 | +| action | function to launch and await | function | () => {} | |
| 84 | + |
| 85 | +### buildReducers |
| 86 | +`buildReducers` create default reducers based on CRUD logic. It creates three functions : pending, fulfilled and rejected. |
| 87 | +- `pending` set the `loadingKey` to `true` and the `errorKey` to `null`. |
| 88 | +- `fulfilled` replaces `itemKey` with the payload (if `itemKey` is not `null`) and the `loadingKey` to `false` |
| 89 | +- `pending` set the `loadingKey` to `false` and the `errorKey` to payload. |
| 90 | + |
| 91 | + |
| 92 | +| Parameters | Description | Type | Default | |
| 93 | +| :------------- | :----------------------------- | :-------- | :-------- | |
| 94 | +| itemKey | the key of the item state | string | 'item' | |
| 95 | +| loadingKey | the key of the loading state | string | 'loading' | |
| 96 | +| errorKey | the key of the error state | string | 'error' | |
| 97 | + |
| 98 | +### buildSlice |
| 99 | +`buildSlice` is a wrapper of [`createSlice`](https://redux-toolkit.js.org/api/createSlice). |
| 100 | + |
| 101 | + |
| 102 | +| Parameters | Description | Type | Default | |
| 103 | +| :-------------------- | :-------------------------------------------- | :-------- | :-------- | |
| 104 | +| name | the name of the slice | string | undefined | |
| 105 | +| modules | array of all modules | array | [] | |
| 106 | +| moduleInitialState | initial state for all modules of the slice | object | {} | |
0 commit comments