How To Use Redux with React Hooks

Use Redux with React Hooks: replace connect with useSelector and useDispatch, available since React Redux 7.1, and refactor a component step by step.

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.

Note: Redux Toolkit (RTK) is now the official, recommended way to write Redux logic. It includes utilities like createSlice and configureStore that simplify the setup shown in this article. See redux-toolkit.js.org for details.


Trevor I. Lasn

Trevor I. Lasn

Tech executive with over a decade of experience programming and building products. Building 0xinsider.com, ceodumps.com and whiteheap.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

React
5 min read

Split a React app's bundle with React.lazy and Suspense, lazy-load routes and CSS, and improve Core Web Vitals, which Google uses as a ranking factor.

Dec 26, 2023
Read article
Next.js
12 min read

How I use the 'Three Layers of Data' architecture pattern for React and Next.js apps to avoid common pitfalls, tech debt, and improve performance

May 4, 2025
Read article
React
11 min read

Testing can be tricky, especially when it comes to handling API calls.

Sep 1, 2024
Read article
Web performance
4 min read

Skeleton loading screens enhance user experience and make your app feel faster

Nov 12, 2020
Read article
React
5 min read

Selectors are a powerful tool to optimize state selection and enhance performance in your React and Redux apps.

Oct 2, 2019
Read article
React
7 min read

Run a React app in Docker: write a Dockerfile, start it with Docker Compose, serve the production build with Nginx and deploy it to a VPS.

Jul 16, 2020
Read article
Next.js
14 min read

Next.js 16 makes params async, Turbopack the default bundler and proxy the new middleware, and removes AMP. Every change with code, updated for 16.4.

Oct 25, 2025
Read article
React
5 min read

Move a class component that fetches API data to React Hooks with useState and useEffect, step by step, and when to use a data-fetching library instead.

Jun 21, 2019
Read article
React
5 min read

React Hooks, added in React 16.8, let you write stateful components without classes. Refactor a class component to a function with useState, step by step.

Jun 16, 2019
Read article

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.