Blog detail

Disadvantages And Advantages Of useState and useReducer in ReactJS

Date: 03-03-2023

Both useState and useReducer are hooks provided by React to manage state in functional components. They have some similarities, but also some differences. In this answer, we’ll discuss the advantages and disadvantages of using useState and useReducer.

Advantages of useState

Ease of Use

useState is very easy to use. It takes a single argument, which is the initial state and returns an array with two elements: the current state and a function to update the state.

Simple State Updates

useState is suitable for simple state updates. If you only need to update one piece of state, useState is a good choice.

Less Boilerplate

useState requires less boilerplate code compared to useReducer. There’s no need to define a separate action object or switch statement.

Disadvantages of useState

Not Suitable for Complex State

useState can become unwieldy if you need to manage complex state, especially if the state is deeply nested. In such cases, it can be difficult to update the state correctly.

State Logic can be Spread out 

If you have multiple state variables in a component, the logic for updating them can be spread out and difficult to manage.

Advantages of useReducer

Suitable for Complex States

useReducer is suitable for managing complex state. It allows you to define a separate action object and switch statement to handle different state updates.

Centralized State Logic

With useReducer, you can centralize the logic for updating the state in one place. This makes it easier to manage complex state.

Easy to Test

useReducer makes it easy to test state updates because the logic for updating the state is defined in a single function.

Disadvantages of useReducer

More Boilerplate

useReducer requires more boilerplate code compared to useState. You need to define a separate action object and switch statement to handle different state updates.

Higher Learning Curve

useReducer can have a higher learning curve compared to useState. You need to understand the concept of actions and how to use them to update the state.

More Code to Write

Because useReducer requires more code to write, it can take longer to implement compared to useState.

useState Hook VS useReducer Hook

When it comes to managing state in React, two hooks that are frequently used are useState and useReducer. Although both hooks serve a similar purpose, they have some key differences that can make one more suitable than the other depending on the use case.

useState Hook

The useState hook is a built-in hook in React that allows us to manage state in functional components. It takes an initial state value as an argument and returns an array with two elements: the current state value and a function to update that value.

Example of how to use the useState Hook

import React, { useState } from ‘react’;

function Counter() {
  const [count, setCount] = useState(0);

  function increment() {
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

In this example, we define a state variable count with an initial value of 0 using the useState hook. We also define a function increment that updates the count variable by incrementing it by 1 whenever the user clicks the “Increment” button.

useReducer Hook

The useReducer hook is another built-in hook in React that allows us to manage state in functional components. It is similar to the useState hook in that it takes an initial state value as an argument, but instead of returning just the current state value and a function to update it, it returns a state value and a dispatch function.

The dispatch function takes an action object as an argument and returns a new state value based on that action. Actions are plain JavaScript objects that describe what happened, and they are used to update the state in a predictable way.

Example of how to use the useReducer Hook

import React, { 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:
      throw new Error();
  }
}

function Counter() {
  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <div>
      <p>Count: {state.count}</p>
      <button onClick={() => dispatch({ type: ‘increment’ })}>Increment</button>
      <button onClick={() => dispatch({ type: ‘decrement’ })}>Decrement</button>
    </div>
  );
}

In this example, we define an initial state object { count: 0 } and a reducer function that takes the current state and an action object as arguments and returns a new state value based on the action. 

We then use the ‘useReducer’ hook to create a state object ‘state’ and a dispatch function ‘dispatch’, which we can use to update the state by passing in action objects.

Comparison between useState and useReducer

Now that we’ve seen examples of both useState and useReducer hooks, let’s compare them to understand their differences.

The useState hook is simple and easy to use for managing simple state values that don’t require complex updates. It is best used for state that only needs to be updated in one way, such as a counter that only needs to be incremented or decremented.

The useReducer hook, on the other hand, is more complex and requires more boilerplate code, but it is better suited for managing more complex state that requires more complex updates. It is best used for state that has multiple possible updates depending on different actions.

Conclusion

Both useState and useReducer have their advantages and disadvantages. Which one you choose depends on your specific use case. If you have simple state updates, useState is a good choice. If you have complex state updates, useReducer is a better choice. In general, useState is easier to use and requires less code, while useReducer is more suitable for managing complex state and centralizing the logic for updating the state.

Tags associated functional components,state management,state update function