1 | # Grid Reducers
|
2 |
|
3 | The grid component state is managed by a group of small reducers, rather than a single object. This makes component updates more efficient, and more maintainable. However, this can result in some confusion when a developer would like to import the grid component with their own custom store.
|
4 |
|
5 | ## Best Practice
|
6 |
|
7 | Generally, developers will be using the grid component, but not the built in store, relying on a store they instantiate for their own application state. This is the preferred design, and you can add the grid `reducers` to your own `rootReducer` very easily.
|
8 |
|
9 | ### Example `./reducers/index.js`
|
10 |
|
11 | ````js
|
12 | import { combineReducers } from 'redux';
|
13 | import { Reducers as gridReducers } from 'react-redux-grid';
|
14 |
|
15 | import myAppReducer from './customReducers/app';
|
16 | import myDataReducer from './customReducers/data';
|
17 |
|
18 | export const rootReducer = combineReducers({
|
19 | myAppReducer,
|
20 | myDataReducer,
|
21 | ...gridReducers
|
22 | });
|
23 |
|
24 | export default rootReducer;
|
25 | ````
|
26 |
|
27 | On the initialization of the store, both custom `reducers` and the grid `reducers` will be joined in the `combineReducer` function, which will enable all grid functionality, as well as custom app functionality. **This is the easiest way to get started.**
|
28 |
|
29 | ### Importing a subset of grid Reducers
|
30 |
|
31 | If you'd like to keep your `rootReducer` as small as possible, and you know you only need a subset of the grid functionality, you can only import the `reducers` you need. The `reducers` have been divided into their individual roles, where a few provide core functionality, and others only serve grid plugins.
|
32 |
|
33 | List of Reducers
|
34 |
|
35 | ````js
|
36 | export const Reducers = {
|
37 | bulkAction,
|
38 | dataSource,
|
39 | editor,
|
40 | errorHandler,
|
41 | grid,
|
42 | loader,
|
43 | menu,
|
44 | pager,
|
45 | selection
|
46 | };
|
47 | ````
|
48 |
|
49 | #### Core Grid Reducers (necessary for all default functionality)
|
50 |
|
51 | 1. dataSource
|
52 | 2. grid
|
53 |
|
54 | #### Optional Reducers, that enable further functionality
|
55 |
|
56 | 1. bulkAction
|
57 | 2. editor
|
58 | 3. errorHandler
|
59 | 4. loader
|
60 | 5. menu
|
61 | 6. pager
|
62 | 7. selection
|
63 |
|
64 | ### Example `./reducers/index.js` where only core reducers are used
|
65 |
|
66 | ````js
|
67 | import { combineReducers } from 'redux';
|
68 | import { Reducers as gridReducers } from 'react-redux-grid';
|
69 |
|
70 | import myAppReducer from './customReducers/app';
|
71 | import myDataReducer from './customReducers/data';
|
72 |
|
73 | export const rootReducer = combineReducers({
|
74 | myAppReducer,
|
75 | myDataReducer,
|
76 | grid: gridReducers.grid,
|
77 | dataSource: gridReducers.dataSource
|
78 | });
|
79 |
|
80 | export default rootReducer;
|
81 | ````
|
82 |
|
83 | ## Dynamic Reducer Keys
|
84 |
|
85 | If the `reducer` names clash with your own reducers, you have the option of changing the keys by providing additional configuration to a grid instantiation.
|
86 |
|
87 | ````js
|
88 | import { Grid, Reducers } from 'react-redux-grid';
|
89 |
|
90 | export const DynamicReducerKeys = {
|
91 | customNameForBulkAction: Reducers.bulkAction,
|
92 | gridDataName: Reducers.dataSource,
|
93 | gridEditorName: Reducers.editor,
|
94 | theseKeysCanBeAnything: Reducers.dataSouce
|
95 | };
|
96 |
|
97 | const config = {
|
98 | data,
|
99 | store,
|
100 | reducerKeys: DynamicReducerKeys
|
101 | };
|
102 |
|
103 | const grid = <Grid { ...config } />;
|
104 | ````
|
105 |
|
106 | ## Nested Grid Reducers
|
107 |
|
108 | If you want to hide grid reducers from the root of your state:
|
109 |
|
110 | ````js
|
111 | import { combineReducers } from 'redux';
|
112 | import { Reducers as rootReducer } from 'react-redux-grid';
|
113 |
|
114 | import myAppReducer from './customReducers/app';
|
115 | import myDataReducer from './customReducers/data';
|
116 |
|
117 | export const rootReducer = combineReducers({
|
118 | myAppReducer,
|
119 | myDataReducer,
|
120 | nested: rootReducer
|
121 | });
|
122 |
|
123 | export default rootReducer;
|
124 |
|
125 |
|
126 | import { Grid, Reducers } from 'react-redux-grid';
|
127 |
|
128 | const config = {
|
129 | data,
|
130 | store,
|
131 | reducerKeys: 'nested'
|
132 | };
|
133 |
|
134 | const grid = <Grid { ...config } />;
|
135 |
|
136 |
|
137 | ````
|
138 |
|
\ | No newline at end of file |