Skip to main content

Command Palette

Search for a command to run...

What is useReducer hook in React?

Published
2 min read
What is useReducer hook in React?
B

Hi there! My name is Bhagya Mudgal, and I am a Software Engineer at KAPX. I have experience in developing decentralized applications on Solana and EVM-compatible blockchains and am passionate about using the latest web technologies to build high-quality web applications. In addition to my professional experience, I am always eager to learn and work with new technologies and share my knowledge with others. I am excited to continue growing and learning in my field, and I look forward to connecting with others who share my interests.

The useReducer hook is a powerful tool in React that allows you to manage state within your components. It is similar to the useState hook, but it is designed for managing state that is more complex or requires more logic to update.

Here's how it works:

import { useReducer } from 'react';

const initialState = { count: 0 };

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

function Counter() {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    <>
      <h1>{state.count}</h1>
      <button onClick={() => dispatch({ type: 'increment' })}>+</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>-</button>
    </>
  );
}

In the example above, we have a simple counter component that uses the useReducer hook. The useReducer hook takes two arguments: a reducer function and an initial state. The reducer function is a pure function that takes in the current state and an action, and returns a new state.

The useReducer hook returns an array with two elements: the current state and a dispatch function. The dispatch function is used to send actions to the reducer, which in turn updates the state.

In our example, we have two buttons that dispatch 'increment' and 'decrement' actions when clicked. These actions are passed to the reducer function, which updates the state accordingly.

One advantage of using the useReducer hook is that it can make it easier to manage complex state updates that involve multiple state values or require asynchronous operations. It also allows you to separate the logic for updating your state from the UI, making your code easier to read and maintain.

Overall, the useReducer hook is a useful tool for managing state in React, and it can be a good alternative to the useState hook in situations where you need more control over your state updates.

Thank you for reading! See you again soon.

Please feel free to reach out to me on Twitter.

You can visit my Portfolio to check out the projects I have worked on.

More from this blog

B

Blog | Bhagya Mudgal

17 posts

Software Engineer @ KAPX | Full Stack Web Developer (MERN) | Web3 | Blockchain

What is useReducer hook in React?