🚀Debugging Microservices & Distributed Systems

Trevor I. Lasn

Staff Software Engineer & Engineering Manager

3 min read

How To Use Redux with React Hooks

Write less code, but better code

With the release of React Redux 7.1.0, we can now use hooks, which offer a modern approach to working with React. Hooks help us write less code while achieving the same functionality. This means we can build our applications faster and more efficiently.

If you’re new to hooks, check out my earlier tutorial, Demystifying React Hooks. It will give you a solid understanding of how hooks work.

Refactoring a Redux Component to Use Hooks

Let’s start with a simple Redux component that uses the traditional class-based approach. We’ll refactor it to use React hooks.

import React, { Component } from "react";
import { connect } from "react-redux";
import { toggleSwitch } from "./UiReducer";
class Toggle extends Component {
render() {
const { ui, toggleSwitch } = this.props;
return (
<input
type="checkbox"
checked={ui.toggle}
onChange={toggleSwitch}
/>
);
}
}
const mapStateToProps = ({ ui }) => ({
ui
});
export default connect(
mapStateToProps,
{ toggleSwitch }
)(Toggle);
Step 1: Convert to a Functional Component

We’ll start by converting the class component to a functional component. This change makes the code shorter and more readable:

import React from "react";
const Toggle = ({ ui, toggleSwitch }) => (
<input
type="checkbox"
checked={ui.toggle}
onChange={toggleSwitch}
/>
);
export default Toggle

In this version, we use a functional component instead of a class. We also directly destructure ui and toggleSwitch from the function parameters.

Step 2: Use useSelector to Access State

To read the state from Redux, we use the useSelector hook:

import React from "react";
import { useSelector } from 'react-redux';
const Toggle = () => {
const ui = useSelector(state => state.ui);
return (
<input
type="checkbox"
checked={ui.toggle}
onChange={toggleSwitch}
/>
);
};
export default Toggle

Here, useSelector replaces mapStateToProps. It provides access to the Redux state directly.

Step 3: Use useDispatch to Dispatch Actions

To dispatch actions, we use the useDispatch hook:

import React from "react";
import { useSelector, useDispatch } from 'react-redux';
const Toggle = () => {
const ui = useSelector(state => state.ui);
const dispatch = useDispatch();
const toggleSwitch = () => {
dispatch({ type: 'TOGGLE' });
};
return (
<input
type="checkbox"
checked={ui.toggle}
onChange={toggleSwitch}
/>
);
};
export default Toggle

With useDispatch, we can call dispatch to send actions. In this case, we dispatch the TOGGLE action when the checkbox is toggled.

Congratulations! You have successfully refactored your component from a class-based approach to using hooks. Be sure to test the component to make sure it works as expected.


Related Articles

If you enjoyed this article, you might find these related pieces interesting as well.

Recommended Engineering Resources

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Imagine where you would be in two years if you actually took the time to learn every day. A little effort consistently adds up, shaping your skills, opening doors, and building the career you envision. Start now, and future you will thank you.


This article was originally published on https://www.trevorlasn.com/blog/how-to-use-redux-with-react-hooks. It was written by a human and polished using grammar tools for clarity.

Interested in a partnership? Shoot me an email at hi [at] trevorlasn.com with all relevant information.