How to set Parent State from Children Component in ReactJS? Last Updated : 07 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To set the parent state from a child component, we use React’s unidirectional data flow. Instead of directly passing data, we pass a function that enables the child to send data back to set the parent state.Prerequisites:React JSuseState HookApproachTo set parent state from child component in React, we will define a callback function in the parent component to update its state. Pass the callback function to the child as a prop. The child calls the callback function via props, sending data to the parent and sets the parent state.Steps to Create React ApplicationStep 1: Create a React application using the following command:npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command:cd foldernameProject Structure: Example: This example demonstrates setting parent state from child component by passing a callback function as a prop to the child component. JavaScript // Filename - App.js import React, { useState } from "react"; import Child from "./child"; const App = () => { const [title, setTitle] = useState(""); return ( <React.Fragment> <h1>{title}</h1> <Child setParentState={setTitle} /> </React.Fragment> ); }; export default App; JavaScript // Filename - child.js import React from "react"; const Child = ({ setParentState }) => { const handleClick = () => { setParentState("Geeks For Geeks"); }; return <button onClick={handleClick}>Reveal Title</button>; }; export default Child; Step to Run Application: Run the application using the following command from the root directory of the project:npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output: Comment More infoAdvertise with us Next Article How to access props.children in a stateless functional component in ReactJS ? P pratikraut0000 Follow Improve Article Tags : ReactJS Technical Scripter 2020 Similar Reads How To Pass Props From Parent to Child Component in ReactJS? ReactJS is a powerful library that helps developers build interactive user interfaces by breaking them into reusable components. One of the essential features of React is the ability to pass data between components using props. In React, props allow parent components to send data or functions to chi 3 min read How to Pass Data From Child Component To Its Parent In ReactJS ? In ReactJS, the flow of data is typically one-way, meaning data is passed from parent to child components using props. However, there are situations where you may need to pass data from a child component back up to its parent component. In this article, we will cover how to pass data from a child co 5 min read How to Call Parent Components's Function from Child Component in React ? In React, it is very important to manage communication between components for building flexible and maintainable applications. Sometime, you need to trigger a function defined in a parent component from a child component. In this article, we'll see how to achieve this in React by passing down functi 3 min read How to access props.children in a stateless functional component in ReactJS ? The {props. children} allows you to create a generic template, which can be modified by a parent when invoked. In this way, a parent component can easily pass whatever is necessary to its child, even generated layout features. Anytime you call a component, it automatically displays whatever it has b 5 min read How to locally manage component's state in ReactJS ? Any component in React JS majorly depends on its props and state to manage data. A component's state is private to it and is responsible for governing its behavior throughout its life. A state is nothing but a structure that records any data changes in a react application. It can be used for storing 2 min read How To Get Previous State In ReactJS Functional Component? To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task.PrerequisitesReact JSReact JS functiona 2 min read Like