Better Programming

Advice for programmers.

Follow publication

React Hooks: Redux Basics Edition

Scott Espinosa
Better Programming
Published in
4 min readNov 9, 2020

--

Redux logo consisting of three swirling lines on the left and the word Redux on the right
Redux: State Management System

React Hooks were introduced to React with the v16.8 release in Feb 2019. Before the release, there were features that were exclusive to class components. With Hooks, functional components now allow the use of state and other React features within them. Hooks were also introduced to be able to reuse stateful logic between components, to simplify complex components that were hard to understand, and to expand features within functional components since classes were confusing to people and machines. In June 2019, React Redux v7.1 was released in order to incorporate Hooks.

Set Up Redux

The setup of Redux will be exactly the same whether or not you’re using Hooks. For the example in this article, we’ll be making a simple counter application (which wouldn’t make much sense for Redux, but we’re using it just for example purposes). First off, you’ll need a couple of packages:

npm install redux
npm install react-redux

The first couple of steps will be to set up the store, reducer, and actions, as seen below:

index.js File
counterActions.js File
counterReducer.js File

Redux Without Hooks

To use Redux without Hooks, the connect() function imported from react-redux would connect the React component with the Redux store, which gives access to state as well as the dispatch functions to change state. To obtain access to those, the parameters of mapStateToProps and mapDispatchToProps are passed through the function, which provides state and the imported dispatch functions as props within the component. Order matters as mapStateToProps will always need to be first and mapDispatchToProps will need to be the second parameter. Both of these will be set up as variables as seen below:

App.js — Counter Application using Redux w/o Hooks

If we want to use Hooks to have the same functionality as the application above, we will no longer need to use the connect() function nor set up variables for mapStateToProps and mapDispatchToProps. Instead, we can use the Hooks available with react-redux, useSelector() and useDispatch().

useSelector()

useSelector() is the equivalent of mapStateToProps as it provides access to the store. This function should typically be a pure function as it is potentially called at multiple times. The entire store would be the argument, so what is pulled from it can be manipulated, as the store is an entire object. When the function is assigned to a variable within a functional component, the state would be accessed through the variable instead of through props. With the counter application, state only consists of the counter that changes and can be set up as below:

App.js — useSelector() setup

useDispatch()

To obtain access to the dispatch functions, we will be able to use the useDispatch() function. This Hook uses a reference to the dispatch function in order to use the dispatch functions as needed. This is also set up by setting the function to a variable, giving it access to be used as a callback, which is needed for our counter application. The dispatch function would then be passed through as an argument, as shown below:

App.js — Counter Application using Redux w/o Hooks

Conclusion

I do find React Hooks to be a lot cleaner code to use, so I would prefer to use React Redux Hooks for any future code. With this counter application, this wouldn’t make the most sense but would be a lot more useful within a larger application. For more information on these Hooks, as I’ve only gone over the simple areas to make a simple functioning application, see the resources below. Keep on Hooking on!

Counter Application

Resources:

  1. “React”, Facebook Open Source, accessed November 7, 2020, https://reactjs.org/

2. “Redux,” Dan Abramov, accessed November 7, 2020, https://redux.js.org/

3. “React-Redux,” Dan Abramov, accessed November 7, 2020, https://react-redux.js.org/

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Scott Espinosa
Scott Espinosa

Written by Scott Espinosa

Software Engineer based out of the San Francisco Bay Area. Flatiron School graduate with 8+ years background in healthcare.

No responses yet

Write a response