How to Work with and Manipulate State in React ?
Last Updated :
23 Jul, 2025
Working with and Manipulating state in React JS makes the components re-render on the UI by Updating the DOM tree. It makes sure to render the latest information and data on the interface.
Prerequisites:
These are the approaches to work with and Manipulate State in React JS.
Steps to Create React App and Installing Module:
Step 1: Creating React Application
npx create-react-app state-demo
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd state-demo
Project Structure:

Steps to Manipulate the State in Class Components:
Now first we will see how to create and manipulate state with class components.
1. Initializing state:
Syntax:
2. Accessing State:
We can access state object anywhere in component with "this.state", the state is local so, don't try to access it from an outside component, if you need it outside then somehow you can pass it as props.
3. Manipulating State:
Class component provides us a setState function we can call it anywhere in the component to manipulate state.
- setState actions are asynchronous, so when we have to do something (ex- fetching dynamic content, changing component internally, etc) strictly after the state has been updated we pass a callback function as a second argument to setState function.
Example: This example implements the state access and manipulation in React JS Class Components.
JavaScript
// Filename - index.js
import React from "react";
import ReactDOM from "react-dom";
class MyComponent extends React.Component {
constructor() {
super();
this.state = {
clicked: 0,
};
}
stateManipulater = () => {
this.setState(
(prevState) => {
return { clicked: prevState.clicked + 1 };
},
() => {
console.log(
"This line will only get " +
"printed after state gets updated"
);
}
);
};
render() {
return (
<div>
<h3>Illustration of Working with State!</h3>
<p>
This Button is associated with an
integer state which increments on click
and UI re-renders accordingly
</p>
<button onClick={this.stateManipulater}>
{`Clicked ${this.state.clicked} Times`}
</button>
</div>
);
}
}
ReactDOM.render(
<MyComponent />, // What to Display
document.getElementById("root") // Where to Display
);
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Explanation: This is the output of the above code, just after clicking on the button, the event gets generated which calls this.setState function to manipulate state, and immediately after that, the callback function passed as a second argument to this.setState executes, and the line gets printed on the console.
Steps to Manipulate the State in Functional Components:
1. Initializing State:
We have useState hook to work around with the state in the functional component, useState receives the initial State as an argument and returns the state variable along with a function that later can be used to set the state associated with it.
Syntax:
const [state, setState] = useState(intialState);// Note:- You are not restricted to name state variable
// as "state" and function as "setState"
2. Accessing State:
We can access state with name directly anywhere in a functional component like “state”.
3. Manipulating State:
The function we receive from useState is used to manipulate the associated state variable.
- Changing state object with a provided object as an argument while calling.
setState(arg);
- When we have to update based on the previous state we pass a function that gets the previous state as a parameter.
setState((prevState)=>{
// Do return a new state object after some manipulations
});
- Unlike class, the functional component doesn't provide us the callback when the state has changed but the useEffect hook can be used to achieve the same, useEffect calls the given function whenever something from the dependency array changes.
Example: This example implements the state access and manipulation in React JS functional components using the React JS Hooks
JavaScript
// Filename - index.js
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
function MyComponent() {
const [state, setState] = useState({ clicked: 0 });
const stateManipulater = () => {
setState((prevState) => {
return { clicked: prevState.clicked + 1 };
});
};
useEffect(() => {
console.log(
"This line will only get " +
"printed after state gets updated"
);
}, [state]);
return (
<div>
<h3>Illustration of Working with State!</h3>
<p>
This Button is associated with an integer
state which increments on click and UI
re-renders accordingly
</p>
<button onClick={stateManipulater}>
{`Clicked ${state.clicked} Times`}{" "}
</button>
</div>
);
}
ReactDOM.render(
<MyComponent />, // What to Display
document.getElementById("root") // Where to Display
);
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Explanation: This is the output of the above code, just after clicking on the button the state gets changed by the event handler, and immediately after that, the component notices that something inside the dependency array of useEffect has been changed hence immediately executes its functionality and we have only written the console log inside it so the line gets printed just after the state change.
Points to remember while working with state:
- Don't ever change the State explicitly, because in this way react will not be able to observe the state changes, and the component's behavior will not get affected, so always use the setState function provided by the component.
- As you understood that state change causes re-render of components hence always keep an eye on the places where the state is changing, sometimes little mistakes may cause extra or in the worst case infinite re-renders, which will consequently make your application slower.
- If you have multiple elements in the state object or array, and you only have to change one element then don't forget to spread the data otherwise it will overwrite the object/array with that single element because state updates as shallow merge.
- After updating the state don't rely on the fact that the state is surely updated, in some cases it may take a little bit of time due to the internal working of REACT, so if you have immediate use of updated state does create your own.
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. Why Use React?Before React, web development faced issues like slow DOM updates and mes
7 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 methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 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 ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 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. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.Stateless (before hooks)
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
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