Author -  Sai gowtham

Intro to useReducer hook in React

In the tutorial we are going to learn about how to manage the state in react by using useReducer hook.

What is Reducer ?

If you know about redux then you are familiar with reducer function.

A Reducer is a JavaScript function which takes the two arguments initial app state, action type and returns back new app state.

useReducer() hook

The useReducer() hook is a alternative to the useState hook but by using this we can manage the complex state logic in our react components.

Example:

In your react app create a new file called reducer.js and add the below code.

reducer.js
export default function reducer(state, action) {
  switch (action.type) {
    case "INC":
      return { ...state, num: state.num + 1 };
    case "DEC":
      return { ...state, num: state.num - 1 };
    default:
      return state;
  }
}

This is a reducer() function with two parameters state,action; every time we are returning a new app state instead of mutating the old state because the reducer is a pure function.

Let’s use our reducer function inside the App.js file.

App.js
import React, { useReducer } from "react";
import reducer from "./reducer";

function App() {
  const [state, dispatch] = useReducer(reducer, { num: 0 });
  return (
    <div>
      {/*accessing the state*/}
      <h1>{state.num}</h1>     {/* dispatching the type of action*/}
      <button onClick={() => dispatch({ type: "INC" })}>Increment</button>      <button onClick={() => dispatch({ type: "DEC" })}>Decrement</button>    </div>
  );
}

export default App;

In above code, we created a App component and imported the reducer function from the reducer.js file and useReducer hook from the react library.

The useReducer() hook accepts two arguments, first argument is reducerfunction and second argument is initial app state, then it returns an array with two values state , dispatch function.

state: current app state.

dispatch: This is a function which is used to update the state by passing type of action.

How to pass the state down to child components ?

We can also pass the useReducer() hook state down to the child components by using React Context API.

import React, { useReducer,createContext} from "react";
import reducer from "./reducer";
import Counter from './counter'

export const MyContext = createContext(null);
function App() {
  const [state, dispatch] = useReducer(reducer, { num: 0 });
  return (
    <MyContext.Provider value={{ state, dispatch }}>      <Counter />
    </MyContext.Provider>
  );
}

export default App;

In the above code we created MyContext by invoking the createContext() function with initial value null, then we wrapped the Counter component with <MyContext.Provider> component by passing the value prop {state,dispatch}.

Now, we can access the state and dispatch function inside the <Counter> component like this.

import React, { useContext } from "react";
import { MyContext } from "./index";

function Counter() {
  const { state, dispatch } = useContext(MyContext);
  return (
    <div>
      {/*accessing the state*/}
      <h1>{state.num}</h1>
     {/* dispatching the type of action*/}
      <button onClick={() => dispatch({ type: "INC" })}>Increment</button>
      <button onClick={() => dispatch({ type: "DEC" })}>Decrement</button>
    </div>
  );
}

export default Counter;

Here we used useContext hook which is used to consume the data passed by the Provider component.

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