What is useReducer hook in React?

What is useReducer hook in React?

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 };
      return state;

function Counter() {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
      <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.

Did you find this article valuable?

Support Bhagya Mudgal by becoming a sponsor. Any amount is appreciated!