Explain State in React.js?

State is a plain JavaScript object used by React to represent an information about the component’s current situation. It’s managed in the component (just like any variable declared in a function). The difference is while a normal variable disappears when their function exits, the state variables are preserved by React.

The React useState Hook allows us to track state in a function component. State generally refers to data or properties that need to be tracking in an application. To use the useState Hook, we first need to import it into our component.

Example:

At the top of your component, import the useState Hook.

import { useState } from "react";

We initialise our state by calling useState in our f unction component.

useState accepts an initial state and returns two values:

  • The current state.
  • A function that updates the state.