1 | initial version |
To transfer data in a callback function from a modal, you can create a function in the parent component and pass it as a prop to the modal component. This function will be called in the modal component whenever data needs to be transferred.
Here’s an example:
const handleData = (data) => {
console.log(`Data received: ${data}`);
};
<Modal onData={handleData} />
const handleOnSave = () => {
const data = { name: 'John Doe', age: 25 };
props.onData(data);
};
In this example, the handleOnSave
function is called when the user clicks the “Save” button in the modal. It creates an object with some data and calls the onData
function with this object as an argument. The parent component’s handleData
function logs this data to the console.
By using this method, you can easily transfer data from a modal to its parent component through a callback function.