-2

I want to send data from child component to parent component in React. Looking up the data, there is only class type and there is no functional document. What syntax can you use to pass data over?

Sour_sr
  • 143
  • 1
  • 5
  • 9

1 Answers1

2

You can use a callback function to use pass data from child to parent.

function Parent(){
    const doSomethingWithDataFromChild(data) {...do something here}
    return <Child passDataToParent={doSomethingWithDataFromChild} />
}


function Child(props) {
    props.passDataToParent(childData);
    return ...
}

Mind that there are some side effects of doing things this way. For the more curated answers you need to provide some more context and/or code examples like what is the use case etc.

Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30