116

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

Martol1ni
  • 4,616
  • 2
  • 27
  • 38
  • 1
    There is nothing wrong in having some components with state. – zerkms Jan 10 '16 at 22:27
  • 4
    That completely depends on where the state is being used. Think of redux stores as being global. Anything that doesn't need to be global can remain private to a component and its children. – azium Jan 10 '16 at 22:27

1 Answers1

144

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.

mjhm
  • 15,731
  • 9
  • 40
  • 55
  • 26
    To follow up with this, the relevant Redux FAQ entry emphasizes that **use of `setState` is completely fine**: http://redux.js.org/docs/faq/OrganizingState.html#organizing-state-only-redux-state – markerikson Jan 12 '17 at 01:08
  • 2
    If you're going to handoff or use server-side rendering, I think you should always use Redux – neaumusic Oct 20 '17 at 19:53
  • Redux FAQ link has been updated to https://redux.js.org/faq/organizing-state – AnandShanbhag May 29 '20 at 14:18