UNPKG

3.64 kBMarkdownView Raw
1# Grid Reducers
2
3The 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
7Generally, 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
12import { combineReducers } from 'redux';
13import { Reducers as gridReducers } from 'react-redux-grid';
14
15import myAppReducer from './customReducers/app';
16import myDataReducer from './customReducers/data';
17
18export const rootReducer = combineReducers({
19 myAppReducer,
20 myDataReducer,
21 ...gridReducers
22});
23
24export default rootReducer;
25````
26
27On 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
31If 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
33List of Reducers
34
35````js
36export 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
511. dataSource
522. grid
53
54#### Optional Reducers, that enable further functionality
55
561. bulkAction
572. editor
583. errorHandler
594. loader
605. menu
616. pager
627. selection
63
64### Example `./reducers/index.js` where only core reducers are used
65
66````js
67import { combineReducers } from 'redux';
68import { Reducers as gridReducers } from 'react-redux-grid';
69
70import myAppReducer from './customReducers/app';
71import myDataReducer from './customReducers/data';
72
73export const rootReducer = combineReducers({
74 myAppReducer,
75 myDataReducer,
76 grid: gridReducers.grid,
77 dataSource: gridReducers.dataSource
78});
79
80export default rootReducer;
81````
82
83## Dynamic Reducer Keys
84
85If 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
88import { Grid, Reducers } from 'react-redux-grid';
89
90export const DynamicReducerKeys = {
91 customNameForBulkAction: Reducers.bulkAction,
92 gridDataName: Reducers.dataSource,
93 gridEditorName: Reducers.editor,
94 theseKeysCanBeAnything: Reducers.dataSouce
95};
96
97const config = {
98 data,
99 store,
100 reducerKeys: DynamicReducerKeys
101};
102
103const grid = <Grid { ...config } />;
104````
105
106## Nested Grid Reducers
107
108If you want to hide grid reducers from the root of your state:
109
110````js
111import { combineReducers } from 'redux';
112import { Reducers as rootReducer } from 'react-redux-grid';
113
114import myAppReducer from './customReducers/app';
115import myDataReducer from './customReducers/data';
116
117export const rootReducer = combineReducers({
118 myAppReducer,
119 myDataReducer,
120 nested: rootReducer
121});
122
123export default rootReducer;
124
125
126import { Grid, Reducers } from 'react-redux-grid';
127
128const config = {
129 data,
130 store,
131 reducerKeys: 'nested'
132};
133
134const grid = <Grid { ...config } />;
135
136
137````
138
\No newline at end of file