It’s OK to useStore with React-Redux

I recently had two members of my team independently confuse themselves with a subtle point in the react-redux bindings. We have a few instances where a component includes callbacks and inside of those callbacks we need to compute some values from our r…


This content originally appeared on DEV Community and was authored by Dylan Grandmont

I recently had two members of my team independently confuse themselves with a subtle point in the react-redux bindings. We have a few instances where a component includes callbacks and inside of those callbacks we need to compute some values from our redux state. We take those values and dispatch an action:

import { useDispatch } from 'react-redux';

function Component() {
  const dispatch = useDispatch();

  function callback() {
    const value = ... // requires us to compute a value from the state
    dispatch(someAction(value))    
  }

  return <div onClick={callback} ... ></div>
}

There are two relevant APIs in react-redux here: useSelector and useStore. useSelector accepts a selector, a function which computes a value from state; when that value changes, the component will re-render.

useStore on the other-hand, provides access to the redux store within component, but it will not re-render the component on any state changes.

The confusion I've seen comes from a small comment within the react-redux docs:

[useStore] should probably not be used frequently. Prefer useSelector() as your primary choice. However, this may be useful for less common scenarios that do require access to the store, such as replacing reducers.

I think this statement makes sense. The primary use-case is to connect your component to a store so that when a particular part of the store changes, the component re-renders. useSelector achieves this and that is the intent of the statement. However, it's easy to misunderstand this as useStore is "discouraged" and this causes trouble.

Suppose you interpret the statement, as my team members did, as "useSelector should always be preferred". In the above example, this will produce a solution like:

import { useDispatch, useSelector } from 'react-redux';

function Component() {
  const dispatch = useDispatch();
  const value = useSelector(someSelector) // compute value from state

  function callback() {
    dispatch(someAction(value))    
  }

  return <div onClick={callback} ... ></div>
}

But this doesn't make sense. We don't need to re-render the component when value changes! Nothing in the rendered output displays the result of value. We only need to evaluate value when the callback is executed. And if value changes frequently in the state, then we are doing a lot of re-renders that we don't need to.

This is one of those "less common scenarios" where we do want access to the store directly:

import { useDispatch, useStore } from 'react-redux';

function Component() {
  const dispatch = useDispatch();
  const store = useStore();

  function callback() {
    const value = someSelector(store.getState())
    dispatch(someAction(value))    
  }

  return <div onClick={callback} ... ></div>
}

This allows the UI to update only when needed and for the correct value to be computed just-in-time, when the callback is executed.


This content originally appeared on DEV Community and was authored by Dylan Grandmont


Print Share Comment Cite Upload Translate Updates
APA

Dylan Grandmont | Sciencx (2021-06-16T17:56:32+00:00) It’s OK to useStore with React-Redux. Retrieved from https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/

MLA
" » It’s OK to useStore with React-Redux." Dylan Grandmont | Sciencx - Wednesday June 16, 2021, https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/
HARVARD
Dylan Grandmont | Sciencx Wednesday June 16, 2021 » It’s OK to useStore with React-Redux., viewed ,<https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/>
VANCOUVER
Dylan Grandmont | Sciencx - » It’s OK to useStore with React-Redux. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/
CHICAGO
" » It’s OK to useStore with React-Redux." Dylan Grandmont | Sciencx - Accessed . https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/
IEEE
" » It’s OK to useStore with React-Redux." Dylan Grandmont | Sciencx [Online]. Available: https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/. [Accessed: ]
rf:citation
» It’s OK to useStore with React-Redux | Dylan Grandmont | Sciencx | https://www.scien.cx/2021/06/16/its-ok-to-usestore-with-react-redux/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.