reduxjs / redux

A JS library for predictable global state management
https://redux.js.org
MIT License
60.88k stars 15.27k forks source link

Where do derived states belong (discussion) #2012

Closed yourGuy closed 8 years ago

yourGuy commented 8 years ago

I have a templateing app with multiple wysiwyg editor components. The App has a preview mode. Each editor component should display it's toolbar if : it's in focus and the application is not in preview mode. My question is : should the component subscribe to the preview mode state and the focus state and have the display logic expressed internally in which case it feels like the component know too much about the application Or should the reducer contain the logic and prep a showToolBar state for each component, derived from the preview state and the focus state in which case it feels like the reducer knows too much about the view logic of a component. Thank you.

markerikson commented 8 years ago

The general recommendation is to keep your state as minimal as possible, and create derived data outside the store. This is usually done using "selector" functions. See http://redux.js.org/docs/recipes/ComputingDerivedData.html and http://redux.js.org/docs/FAQ.html#react-mapstate-speed .

So, I would suggest defining selectors which combine the actual data and the view-specific data for use in your components, as needed.

rickhanlonii commented 8 years ago

New location for the second link: http://redux.js.org/docs/faq/ReactRedux.html#react-mapstate-speed