How to Update Parent State in ReactJS ?
Last Updated :
01 Oct, 2024
Updating the parent state in React involves passing a callback function as a prop to the child that triggers the state change and updates the data in the state.
Prerequisites:
Approach
To update parent state in React we will pass a function as a prop to the child component. Then trigger the update funtion from the child component and the state in the parent component will be directly updated. Updating state for Class as well as Functional Components are given below.
Steps to Create 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
Project Structure:
Project StructureUpdating Parent State in Class Component
To update the parent state in class component we will pass the this.state to display data and handleUpdate funtion as props to trigger the state change. The handleUpdate funtion triggers this.setState to change the state in class component.
Example: This example updates the Parent state by button click in child component for react class components.
JavaScript
// Filename: App.js
import React, { Component } from "react";
class App extends Component {
state = {
text: "click me"
};
// Function to update state
handleUpdate = (newtext) => {
this.setState({ text: newtext });
};
render() {
return (
<div>
<Child
text={this.state.text}
// Passing a function to child
updateState={this.handleUpdate}
></Child>
</div>
);
}
}
class Child extends Component {
render() {
return (
<button
// Using parent props to update parent state
onClick={() => this.props.updateState("parent state changed")}
>
{this.props.text}
</button>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Updating Parent State in Functional Component
To update the parent state in functional component we will create useState variable. we will pass the text to display data and setText funtion as props to trigger the state change. We can directly pass the setText funtion as a prop, hendleUpdate is used to improve the readability of the code.
Example: This example update the parent state by passing the useState text variable and its setText funtion as a prop to the child component in case of funtional component.
JavaScript
// Filename - App.js
import React, { useState } from "react";
const App = () => {
const [text, setText] = useState("click me");
// Function to update state
const handleUpdate = (newText) => {
setText(newText);
};
return (
<div>
<Child
text={text}
// Passing a function to child
updateState={handleUpdate}
/>
</div>
);
};
const Child = ({ text, updateState }) => {
return (
<button
// Using parent props to update parent state
onClick={() => updateState("parent state changed")}
>
{text}
</button>
);
};
export default App;
Output:
Similar Reads
How to Update Nested State Properties in ReactJS? Updating Nested State Properties in React is an important part of State Management. Nested states are objects containing one or more properties as arrays and objects.Prerequisites:React JSState in ReactSpread OperatoruseState hookHow to Updated Nested State in ReactTo update nested state in React we
3 min read
How to Update an Object with setState in ReactJS? setState in React is used to update the component state. Updating an object with setState involves modifying only specified properties of object without modifying the complete object. Directly updating the object can lead to the loss of data and unwanted results.Prerequisites:ReactJSsetStateApproach
2 min read
How to lift state two levels up in ReactJS ? We want to lift the state two levels up such that when an event occurs at the lower level component then it should affect the state two levels up. For example, there are three components X, Y, and Z. X is the parent of Component Y, and Component Y is the parent of Component Z. Prerequisites:NodeJS o
2 min read
How to Use Flux to Manage State in ReactJS? State management in ReactJS is important for building dynamic and responsive applications. Flux, an architecture for managing state, provides a structured approach to handle data flow and state changes efficiently in React applications. In this article, we will explore the Flux to Manage State in Re
5 min read
How to update the State of a component in ReactJS ? To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM & Node.jsReact JSReact StateReact
3 min read
State Management with useState Hook in React useState is a built-in hook that empowers functional components to manage state directly, eliminating the need for class-based components or external state management libraries for simple use cases. It provides an easy mechanism to track dynamic data within a component, enabling it to React to user
3 min read