Author -  Sai gowtham

React Redux hooks tutorial with examples

In this tutorial, we are going to learn about how to use react hooks with a redux store and perform actions.

React hooks API allows us to use state and lifecycle functionalities in functional components.

This tutorial assumes that you already have some basic knowledge about redux, if don’t know about it you can check out my redux tutorial.

Getting started

First, we are creating a new react project by using the create-react-app cli.

Open your terminal and run the below command.

npx create-react-app redux-hooks

This above command will download the react app-related files in the “redux-hooks” folder.

Now, we need to change our working directory to “redux-hooks” folder.

cd redux-hooks

Installing redux and react-redux libraries

Run the below commands in your terminal to install both libraries.

npm i redux react-redux

Setting up redux store

Now open your redux-hooks folder in your favorite code editor.

create a new file called store.js in your src folder and add the below code.

store.js
function reducer(state = { num: 0 }, action) {
  switch (action.type) {
    case "INCREMENT":      return {
        ...state,
        num: state.num + action.step
      };
    case "DECREMENT":      return {
        ...state,
        num: state.num - action.step
      };
    default:
      return state;
  }
}

export default reducer;

In the above code, we have created a reducer() function with two action types INCREMENT and DECREMENT.

Now open your index.js and add the below code.

index.js
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";import { createStore } from "redux";import "./styles.css";
import reducer from "./store.js";import App from "./app";

const store = createStore(reducer);
const rootElement = document.getElementById("root");
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  rootElement
);

With this our redux setup is complete, we can now access the redux store from the functional components using react hooks.

useSelector hook (accessing state)

The useSelector hook is similar to mapStatetoprops by using this hook we can access the entire redux store state.

The useSelector hook will also subscribe to the redux store.

App.js
import React from "react";
import { useSelector } from "react-redux";
function App() {
  const counter = useSelector(state => state);
  return (
    <div className="App">
      <h1>{counter.num}</h1>    </div>
  );
}

export default App;

In the above code, we first imported useSelector hook from the react-redux library.

The useSelector() will take the function as an argument and the returns the redux store state.

useDispatch hook (Dispatching actions)

The useDispatch() hook is used to dispatch the actions.

App.js
import React from "react";
import { useSelector } from "react-redux";
import { useDispatch } from "react-redux";
function App() {
  const counter = useSelector(state => state);
  const dispatch = useDispatch();
  return (
    <div className="App">
      <h1>{counter.num}</h1>
      <button
        onClick={() =>          dispatch({            type: "INCREMENT",            step: 1          })        }
      >
        Increment
      </button>
    </div>
  );
}

export default App;

Here we are dispatching the action type INCREMENT whenever we click on a Increment button.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY