Pass data from child to parent component in React
In this article, we are discussing how to Pass data from the child to the parent component in React. Basically, React is designed to have a unidirectional data flow, which means data from parent to child component. But in many cases, we may want to pass data from the child to the parent component. For example, we may want to pass data from the child component to the parent component on a button click in the child component. We can follow the below steps to pass data from the child to the parent in React.
- Create a function that can update the state in the parent component.
- Pass this function into the child component as a prop.
- Call this function from the child component and pass the data as arguments.
- Access the data as arguments in the function in the parent component.
Let us dive deep into the above steps with examples.
import { useState } from 'react';
const ParentComponent = () => {
const [message, setMessage] = useState("Hello");
const changeMessage = (newMessage) => {
setMessage(newMessage);
}
return (
<>
<label>{message}</label>
<ChildComponent changeMessage={changeMessage}/>
</>
);
};
const ChildComponent = ({changeMessage}) => {
const ClickHandler = () => {
changeMessage("Goodbye!");
}
return (
<button onClick={ClickHandler}>Show Goodbye</button>
);
};
In the above code example, the initial state in message
variable in the parent component was "hello" and it was displayed in the label. The parent component defines the function changeMessage
which takes newMessage
as arguments and the message
state gets updated. Data can be passed from the parent to the child component via props. Any properties of the parent component, like variables or functions can be passed to the child component as props. So, this changeMessage
function can be passed into the child component as a prop, so that the child component can call this function and can pass any data into the parent.
In the child component, we destructure the props so that we can get changeMessage
function directly without the prop variable. When the button has clicked the changeMessage
function was invoked from its clickHandler and passed the data as arguments. The changeMessage
function actually updates the state of the parent component from the child component.
We can also pass an event object as a parameter into the function to use it in the parent component.
We can use this method to pass anything from a child component to a parent component in React.
In large applications, sharing data with multiple components may be difficult by passing functions as props. In such cases, we can use a global store such as redux and the context API.
Conclusion
To pass data from child to parent component in React, define a function in the parent component that can take arguments and update the state in the parent component. Pass this function into the child component as a prop. In any event, invoke the function from the child component and pass data into the parent component as an argument.
On datainfinities.com, Read articles all around JavaScript, React, Node.js, PHP, Laravel, and Shopify.
Related Blogs
Adjacent JSX elements must be wrapped in an enclosing tag
useHref() may be used only in the context of a Router component
Why useEffect is running twice in React?
Cannot read property of undefined in JavaScript
Can't Perform a React State Update on an Unmounted Component
How to create a scroll to top button in React
React objects are not valid as a react child
Invalid hook call. Hooks can only be called inside the body of a function component
How to pass event and parameter onClick in React
Explore All Blogs