Want to compose redux reducers with dependent state? Create a selector to access your state and return derived data.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import { createSelector } from 'reselect' const widgetsSelector = state => state.widgets; const selectedWidgetsSelector = state => state.selection.widgets; function minCoordinateSelector(widgets, selected) { const x_list = selected.map((widget) => { return widgets[widget].x; }); const y_list = selected.map((widget) => { return widgets[widget].y; }); return { x: Math.min(...x_list), y: Math.min(...y_list) }; } const coordinateSelector = createSelector( widgetsSelector, selectedWidgetsSelector, (widgets, selected) => { return minCoordinateSelector(widgets, selected); } ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.