What is useState() in React ?
Last Updated :
09 Jan, 2025
The useState() is a Hook that allows you to have state variables in functional components . so basically useState is the ability to encapsulate local state in a functional component. React has two types of components, one is class components which are ES6 classes that extend from React and the other is functional components. Class components a Component and can have state and lifecycle methods: class Message extends React. The useState hook is a special function that takes the initial state as an argument and returns an array of two entries. UseState encapsulate only singular value from the state, for multiple state need to have useState calls.
Syntax: The first element is the initial state and the second one is a function that is used for updating the state.
const [state, setState] = useState(initialstate)
We can also pass a function as an argument if the initial state has to be computed. And the value returned by the function will be used as the initial state.
const [sum, setsum] = useState(function generateRandomInteger(){5+7);})
The above function is oneline function which computes the sum of two numbers and will be set as the initial state.
Importing: To use useState you need to import useState from react as shown below:
import React, { useState } from "react"
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Example: Below is the implementation of useState() function.
App.js
JavaScript
import React, { useState } from "react";
function App(props) {
const [count, setRandomCount] =
useState(function generateRandomInteger() {
return Math.floor(Math.random() * 100);
});
function clickHandler(e) {
setRandomCount(Math.floor(Math.random() * 100));
}
return (
<div style={{margin: 'auto', width: 100, display: 'block'}}>
<h1> {count} </h1>
<p>
<button onClick={clickHandler}> Click </button>
</p>
</div>
);
}
export default App
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:

Explanation: First count variable is initialized with a random number using a random function and setRandomCount is used for updating the state of the count. Every time we click on the button onClick it calls the clickHandler function which sets the count variable again with a random number.
Similar Reads
React useState Hook The useState hook is a function that allows you to add state to a functional component. It is an alternative to the useReducer hook that is preferred when we require the basic update. useState Hooks are used to add the state variables in the components. For using the useState hook we have to import
5 min read
What is React? React JS is a free library for making websites look and feel cool. It's like a special helper for JavaScript. People from Facebook and other communities work together to keep it awesome and up-to-date. React is Developed by Facebook, React is a powerful JavaScript library used for building user inte
6 min read
Getting Started with React ReactJS, often referred to as React, is a popular JavaScript library developed by Facebook for building user interfaces. It emphasizes a component-based architecture, where UIs are built using reusable components. React uses a declarative syntax to describe how UIs should look based on their state,
10 min read
Lifting State Up in ReactJS In ReactJS, "lifting state up" is a fundamental concept that plays an important role in managing the state efficiently across different components. When building React applications, it is common for multiple components to need access to the same piece of data or state. In such cases, instead of dupl
4 min read
ReactJS setState() In React, setState() is an essential method used to update the state of a component, triggering a re-render of the component and updating the UI. This method allows React to efficiently manage and render changes in the component's state.What is setState()?setState() is a method used to update the st
7 min read