Managing Complex State with useState
Last Updated :
23 Jul, 2025
Managing the state in React applications is very important when you want to create dynamic and interactive user interfaces. In this article, we will be going to learn how we can use useState( A React Hook ) to manage the complex states in React applications we generally do it using the redux library of React ( Redux is a React library that allows us to manage to react complex state efficiently and cleanly).
What is useState Hook?
React useState() hook allows one to declare a state variable inside a function. It should be noted that one use of useState() can only be used to declare one state variable. It was introduced in version 16.8.
Importing React useState Hook:
To import the useState hook, write the following code at the top level of your component
import { useState } from "react";
Structure of React useState hook
This hook takes some initial state and returns two value. The first value contains the state and the second value is a function that updates the state. The value passed in useState will be treated as the default value
React useState Hook Syntax:
const [val, setVal] = useState(0);
What is Complex State Management ?
Sometime we need to manage different states in different components. Managing the different states changes in different components of react app is known as complex state management. More number of component in the react app makes the app more complex and it is difficult to manage the state between them which are common in many of the components.
To manage this state management there are many libraries that you can learn about it named Redux, Context API.
Now we will see how we can manage complex states using useState.
Steps to Create React Project
Step 1: Create a React Application by entering the Command.
npx create-react-app gfg
Step 2: After the Neccesary folders created, enter into the folder using command.
cd gfg
Step 3: Now Install the necessary dependencies using command
npm install
Step 4: You can see the current basic setup by running it using common
npm start
The update dependencies in package.json file will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Examples of Complex State Management
Example 1: In this example we will handle the state of every input field of the form with the help of single useState
Folder Structure:

Example Code:
JavaScript
// App.js
import Form from "./Form/Form";
function App() {
return (
<div className="App">
<Form />
</div>
);
}
export default App;
JavaScript
// Form/form.js
import React from 'react';
import useUserState from '../Form/common'
function Form() {
const { Data, setData } = useUserState();
const handleChange = (event) => {
setData((prev) => ({
...prev,
[event.target.name]: event.target.value,
}))
};
const handleDetails = (event) => {
setData((prev) => ({
...prev,
details: {
...prev.details,
[event.target.name]: event.target.value,
}
}))
}
const handleSubmit = (e) => {
e.preventDefault();
alert('Form Data Submitted');
};
return (
<div >
<form style={{ display: "flex", flexDirection: "column",
width: "300px", gap: "20px", padding: "20px" }}>
<label>
Username:
</label>
<input type="text" name="username" value={Data.details.username}
onChange={handleDetails} />
<label>
Email:
</label>
<input type="email" name="email" value={Data.email}
onChange={handleChange} />
<label>
Password:
</label>
<input type="password" name="password" value={Data.password}
onChange={handleChange} />
<label>
Age:
</label>
<input type="number" name="age" value={Data.details.age}
onChange={handleDetails} />
<button type="submit" onClick={handleSubmit}>Submit</button>
</form>
</div>
);
}
export default Form;
JavaScript
// Form/common.js
import { useState } from 'react';
const useUserState = () => {
const [Data, setData] = useState({
email: '',
password: '',
details: {
username: '',
age: '',
}
});
return { Data, setData };
};
export default useUserState;
Output:
Example 2: In this example we are managing two counter at the same sime with the help of single useState.
Folder Structure:
Folder StructureExample Code:
JavaScript
// App.js
import Counter from "./Counter/Counter";
function App() {
return (
<div className="App">
<Counter />
</div>
);
}
export default App;
JavaScript
// Counter/Counter.js
import React from 'react';
import useUserState from './common'
function Counter() {
const { items, setItems } = useUserState();
const updateQuantity = (itemId, newQuantity) => {
setItems((prevItems) =>
prevItems.map((item) =>
item.id === itemId ?
{ ...item, quantity: newQuantity } : item
)
);
};
return (
<div style={{ display: "flex", flexDirection: "column", gap: "10px" }}>
<div>
{items[0].quantity}
<br />
<button onClick={() => updateQuantity(items[0].id,
items[0].quantity + 1)}>Increase</button>
</div>
<div>
{items[1].quantity}
<br />
<button onClick={() => updateQuantity(items[1].id,
items[1].quantity + 1)}>Increase</button>
</div>
</div>
);
}
export default Counter;
JavaScript
// Counter/common.js
import { useState } from 'react';
const useUserState = () => {
const [items, setItems] = useState([
{ id: 1, name: 'Nitin', quantity: 2 },
{ id: 2, name: 'Sneha', quantity: 4 },
]);
return { items, setItems };
};
export default useUserState;
Output:
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects