courses.reviews - My new website for finding the best coding courses, with reviews & exclusive discounts. It's free!

Published
3 min read
Archived

Trevor I. Lasn

Staff Engineer, EM & Entrepreneur

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.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.



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.