HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptreactCritical

Call child method from parent

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
fromparentmethodchildcall

Problem

I have two components:

  • Parent component



  • Child component



I was trying to call Child's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      
        Click
      
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      Hello
    );
  }
}


Is there a way to call Child's method from Parent?

Note: Child and Parent components are in two different files.

Solution

First off, let me express that this is generally not the way to go about things in React land. Usually what you want to do is pass down functionality to children in props, and pass up notifications from children in events (or better yet: dispatch).

But if you must expose an imperative method on a child component, you can use refs. Remember this is an escape hatch and usually indicates a better design is available.

Previously, refs were only supported for Class-based components.
With the advent of React Hooks, that's no longer the case

Modern React with Hooks (v16.8+)



const { forwardRef, useRef, useImperativeHandle } = React;

// We need to wrap component in
forwardRef in order to gain
// access to the ref object that is assigned using the
ref prop.
// This ref is passed as the second parameter to the function component.
const Child = forwardRef((props, ref) => {

// The component instance will be extended
// with whatever you return from the callback passed
// as the second argument
useImperativeHandle(ref, () => ({

getAlert() {
alert("getAlert from Child");
}

}));

return Hi;
});

const Parent = () => {
// In order to gain access to the child component instance,
// you need to assign it to a
ref, so we call useRef() to get one
const childRef = useRef();

return (


childRef.current.getAlert()}>Click

);
};

ReactDOM.render(
,
document.getElementById('root')
);







Documentation for useImperativeHandle() is here:

useImperativeHandle customizes the instance value that is exposed to parent components when using ref.

Legacy API using Class Components (>= react@16.4)



const { Component } = React;

class Parent extends Component {
constructor(props) {
super(props);
this.child = React.createRef();
}

onClick = () => {
this.child.current.getAlert();
};

render() {
return (


Click

);
}
}

class Child extends Component {
getAlert() {
alert('getAlert from Child');
}

render() {
return Hello;
}
}

ReactDOM.render(, document.getElementById('root'));







Callback Ref API

Callback-style refs are another approach to achieving this, although not quite as common in modern React:



const { Component } = React;
const { render } = ReactDOM;

class Parent extends Component {
render() {
return (

{ this.child = instance; }} />
{ this.child.getAlert(); }}>Click

);
}
}

class Child extends Component {
getAlert() {
alert('clicked');
}

render() {
return (
Hello
);
}
}

render(
,
document.getElementById('app')
);



Context

Stack Overflow Q#37949981, score: 1415

Revisions (0)

No revisions yet.