Call child method from parent

前端 未结 16 2331
长发绾君心
长发绾君心 2020-11-21 22:23

I have two components.

  1. Parent component
  2. Child component

I was trying to call child\'s method from Parent, I tried this way but couldn\

16条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 23:16

    Here my demo: https://stackblitz.com/edit/react-dgz1ee?file=styles.css

    I am using useEffect to call the children component's methods. I have tried with Proxy and Setter_Getter but sor far useEffect seems to be the more convenient way to call a child method from parent. To use Proxy and Setter_Getter it seems there is some subtlety to overcome first, because the element firstly rendered is an objectLike's element through the ref.current return =>

    's specificity. Concerning useEffect, you can also leverage on this approach to set the parent's state depending on what you want to do with the children.

    In the demo's link I have provided, you will find my full ReactJS' code with my draftwork inside's so you can appreciate the workflow of my solution.

    Here I am providing you my ReactJS' snippet with the relevant code only. :

    import React, {
      Component,
      createRef,
      forwardRef,
      useState,
      useEffect
    } from "react"; 
    
    {...}
    
    // Child component
    // I am defining here a forwardRef's element to get the Child's methods from the parent
    // through the ref's element.
    let Child = forwardRef((props, ref) => {
      // I am fetching the parent's method here
      // that allows me to connect the parent and the child's components
      let { validateChildren } = props;
      // I am initializing the state of the children
      // good if we can even leverage on the functional children's state
      let initialState = {
        one: "hello world",
        two: () => {
          console.log("I am accessing child method from parent :].");
          return "child method achieve";
        }
      };
      // useState initialization
      const [componentState, setComponentState] = useState(initialState);
      // useEffect will allow me to communicate with the parent
      // through a lifecycle data flow
      useEffect(() => {
        ref.current = { componentState };
        validateChildren(ref.current.componentState.two);
      });
    
    {...}
    
    });
    
    {...}
    
    // Parent component
    class App extends Component {
      // initialize the ref inside the constructor element
      constructor(props) {
        super(props);
        this.childRef = createRef();
      }
    
      // I am implementing a parent's method
      // in child useEffect's method
      validateChildren = childrenMethod => {
        // access children method from parent
        childrenMethod();
        // or signaling children is ready
        console.log("children active");
      };
    
    {...}
    render(){
           return (
              {
                // I am referencing the children
                // also I am implementing the parent logic connector's function
                // in the child, here => this.validateChildren's function
              }
              
            
    ) }

提交回复
热议问题