Should you ever use this.setState() when using redux?

ReactjsFluxRedux

Reactjs Problem Overview


Should you ever use this.setState() when using redux? Or should you always be dispatching actions and relying on props?

Reactjs Solutions


Solution 1 - Reactjs

Clear uses of setState would be for UI components that have local display state, but aren't relevant for the global application. For example a boolean that represents whether a specific dropdown menu is actively displayed doesn't need to be in global state, so it's more conveniently controlled by the menu component's state.

Other examples might include the collapse/expand state of lines in an accordion display of a hierarchy. Or possibly the currently selected tab in tab navigation. However in both of these examples you might still choose to handle UI state globally. For example this would be necessary if you wanted to persist the expand/collapse state in browser storage so that it would be preserved by page refresh.

In practice it's usually easiest to implement such UI elements with local state, and refactor them into global state as needed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartol1niView Question on Stackoverflow
Solution 1 - ReactjsmjhmView Answer on Stackoverflow