How to pass the data from child to parent? when parent is class based and child is functional based

前端 未结 3 622
北海茫月
北海茫月 2021-01-23 20:55

I want to pass the data which is anything like, array, strings, numbers into the App(Parent) Component) from the Child1(Child) components.

There\'s a parent who is class

3条回答
  •  野性不改
    2021-01-23 21:23

    In your child component if you want to send some data to the parent upon clicking a button for example:

    import React from 'react'
    
    const Child1 = React.memo((props) => {
        return (
            
    ); }) export default Child1;

    now you can assign a function to the clicked prop in your parent component and that gets called when the button in the child component is clicked:

    class App extends Component {
      state = { message: "" };
    
      callbackFunction = (event) => {
        this.setState({
          message: event.target.value
        });
      }
    
      clicked = (data) => {
       console.log(data);
      }
    
      render() {
        return (
          
    ); } }

提交回复
热议问题