- In React, data can be passed from a child component to a parent component by using a
callback function - The parent component can pass a
function as a propto the child component, and the child component can then invoke that function with any data it wants to send back to the parent.
Let's go through the steps of how to pass data from a child to a parent component using this approach:
Parent Component:
Define the callback function in the parent component and pass it as a prop to the child component.
// ParentComponent.tsx
import React, { useState } from "react";
import ChildComponent from "./ChildComponent";
const ParentComponent: React.FC = () => {
// State to hold the data received from the child component
const [dataFromChild, setDataFromChild] = useState<string>("");
// Callback function to receive data from the child component
const handleDataFromChild = (data: string) => {
setDataFromChild(data);
};
return (
<div>
<h1>Data from Child: {dataFromChild}</h1>
<ChildComponent sendDataToParent={handleDataFromChild} />
</div>
);
};
export default ParentComponent;Child Component:
In the child component, we receive the callback function as a prop and use it to send data back to the parent component.
// ChildComponent.tsx
import React from "react";
interface Props {
sendDataToParent: (data: string) => void; // The callback function type
}
const ChildComponent: React.FC<Props> = ({ sendDataToParent }) => {
// Function to send data to the parent component
const sendData = () => {
const dataToSend = "Hello from the Child!";
sendDataToParent(dataToSend); // Call the callback function with data
};
return (
<div>
<button onClick={sendData}>Send Data to Parent</button>
</div>
);
};
export default ChildComponent;Explanation:
- In the parent component, we define a callback function
handleDataFromChildthat takes adataparameter. This function will be responsible for updating the statedataFromChildwith the data received from the child component. - We pass this
handleDataFromChildfunction as a prop calledsendDataToParentto the child component. - In the child component, we receive the
sendDataToParentprop and store it in thePropsinterface. This prop is a function that takes adataparameter and returnsvoid. - When the button in the child component is clicked, the
sendDatafunction is invoked. Inside this function, we create a variabledataToSendwith the data we want to send back to the parent. - We then call the
sendDataToParentcallback function, passingdataToSendas an argument. This sends the data back to the parent component. - The
handleDataFromChildfunction in the parent component is triggered with the data from the child, and it updates thedataFromChildstate with the received data. - Finally, the updated
dataFromChildis displayed in the parent component.
This is how you can pass data from a child component to a parent component in React using callback functions. By understanding this concept, you can efficiently manage data flow between components and build more complex applications. Remember that props are read-only, and to pass data from child to parent, you'll need to pass functions as props and call them with the data you want to send back.