Call child method from parent

前端 未结 16 2093
长发绾君心
长发绾君心 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:07

    I wasn't satisfied with any of the solutions presented here. There is actually a very simple solution that can be done using pure Javascript without relying upon some React functionality other than the basic props object - and it gives you the benefit of communicating in either direction (parent -> child, child -> parent). You need to pass an object from the parent component to the child component. This object is what I refer to as a "bi-directional reference" or biRef for short. Basically, the object contains a reference to methods in the parent that the parent wants to expose. And the child component attaches methods to the object that the parent can call. Something like this:

    // Parent component.
    function MyParentComponent(props) {
    
       function someParentFunction() {
          // The child component can call this function.
       }
    
       function onButtonClick() {
           // Call the function inside the child component.
           biRef.someChildFunction();
       }
    
       // Add all the functions here that the child can call.
       var biRef = {
          someParentFunction: someParentFunction
       }
    
       return 
    ; } // Child component function MyChildComponent(props) { function someChildFunction() { // The parent component can call this function. } function onButtonClick() { // Call the parent function. props.biRef.someParentFunction(); } // Add all the child functions to props.biRef that you want the parent // to be able to call. props.biRef.someChildFunction = someChildFunction; return
    ; }

    The other advantage to this solution is that you can add a lot more functions in the parent and child while passing them from the parent to the child using only a single property.

    An improvement over the code above is to not add the parent and child functions directly to the biRef object but rather to sub members. Parent functions should be added to a member called "parent" while the child functions should be added to a member called "child".

    // Parent component.
    function MyParentComponent(props) {
    
       function someParentFunction() {
          // The child component can call this function.
       }
    
       function onButtonClick() {
           // Call the function inside the child component.
           biRef.child.someChildFunction();
       }
    
       // Add all the functions here that the child can call.
       var biRef = {
          parent: {
              someParentFunction: someParentFunction
          }
       }
    
       return 
    ; } // Child component function MyChildComponent(props) { function someChildFunction() { // The parent component can call this function. } function onButtonClick() { // Call the parent function. props.biRef.parent.someParentFunction(); } // Add all the child functions to props.biRef that you want the parent // to be able to call. props.biRef { child: { someChildFunction: someChildFunction } } return
    ; }

    By placing parent and child functions into separate members of the biRef object, you 'll have a clean separation between the two and easily see which ones belong to parent or child. It also helps to prevent a child component from accidentally overwriting a parent function if the same function appears in both.

    One last thing is that if you note, the parent component creates the biRef object with var whereas the child component accesses it through the props object. It might be tempting to not define the biRef object in the parent and access it from its parent through its own props parameter (which might be the case in a hierarchy of UI elements). This is risky because the child may think a function it is calling on the parent belongs to the parent when it might actually belong to a grandparent. There's nothing wrong with this as long as you are aware of it. Unless you have a reason for supporting some hierarchy beyond a parent/child relationship, it's best to create the biRef in your parent component.

提交回复
热议问题