React JS useState Hook

What is React JS useState Hook and How to use?

React, the widely used JavaScript library for building user interfaces, offers a powerful way to manage component state.

One of the fundamental tools for state management in functional components is the useState hook.

In this article, we will explore the useState hook in detail, covering its syntax, use cases, and best practices for effectively managing state in your React applications.

What is useState in React?

useState is a Hook in React that allows functional components to manage local state. It was introduced in React 16.8 as part of the Hooks API.

Before hooks, state management was primarily associated with class components. With useState, functional components can now maintain state as well.

“useState” Hook Syntax:

The basic syntax of the useState hook is straightforward. It returns an array with two elements:

  1. The current state and
  2. A function to update that state.

Here’s how it’s typically used:

import { useState } from 'react';

function Counter() {
    const [count, setCount] = useState(0);
    return (
            <h2>Count: {count}</h2>
            <button onClick={() => setCount(count + 1)}>Increment</button>

export default Counter;

In the example above, we initialize the count state variable to 0 using useState. The setCount function, returned by useState, allows us to update the count state.

React JS Counter increment using usestate hook

Use Cases for useState:

  • Managing Component State:

    The primary use case for useState is managing the state within a functional component. You can use it to store and update any data that should trigger a re-render when it changes.

    const [name, setName] = useState("John");
  • Controlled Components:

    useState is often used in combination with form elements to create controlled components.

    This is where the component’s state determines the value of form inputs and other UI elements.

    const [inputValue, setInputValue] = useState('');
    // ...
      onChange={(e) => setInputValue(}
  • Toggling UI Elements:

    You can use useState to toggle UI elements’ visibility or manage various component states.

    const [isOpen, setIsOpen] = useState(false);
    // ...
    <button onClick={() => setIsOpen(!isOpen)}>
      {isOpen ? 'Close' : 'Open'}
    {isOpen && <div>Content to show/hide</div>}

Best Practices for Using useState:

To make the most of the useState hook in React, consider the following best practices:

  • Name Variables Descriptively:

    Give your state variables and state updater functions meaningful names. This makes your code more self-explanatory and easier to understand.

    const [isLoading, setIsLoading] = useState(true);
  • Use Multiple useState Calls

    Rather than combining multiple states into one useState call, use separate useState calls for each state variable. This makes your code more modular and easier to manage.

    const [name, setName] = useState('John');
    const [age, setAge] = useState(30);
  • Avoid Complex State Objects:

    It’s usually best to use multiple useState calls for simpler state management instead of maintaining a single state object. This can help prevent unnecessary re-renders.

    // Prefer this
    const [name, setName] = useState('John');
    const [age, setAge] = useState(30);
    // Over this
    const [person, setPerson] = useState({ name: 'John', age: 30 });
  • Use Functional Updates:

    When you need to update state based on the previous state, use functional updates. This ensures that you’re working with the most up-to-date state.

    // Standard state update
    setCount(count + 1);
    // Functional state update
    setCount((prevCount) => prevCount + 1);


The useState hook is a fundamental part of React’s Hooks API, allowing functional components to manage local state.

By understanding its syntax, use cases, and best practices, you can efficiently manage component state, create controlled components, and toggle UI elements with ease.

useState empowers developers to write cleaner and more maintainable code in functional components, making it a crucial tool in modern React development.