question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to call child component method from parent?

See original GitHub issue

Child.js

import s from './Child.css';

class Child extends Component {
 getAlert() {
    alert('clicked');
 }
 render() {
  return (
    <h1 ref="hello">Hello</h1>
  );
 }
}

export default withStyles(s)(Child);

Parent.js

class Parent extends Component {
 render() {
  onClick() {
    this.refs.child.getAlert() // undefined
  }
  return (
    <div>
      <Child ref="child" />
      <button onClick={this.onClick.bind(this)}>Click</button>
    </div>
  );
 }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:60
  • Comments:41 (4 by maintainers)

github_iconTop GitHub Comments

768reactions
frenzzycommented, Oct 11, 2016

For example you can use Refs to Components approach like so:

/* Child.js */
import React from 'react'
import withStyles from 'isomorphic-style-loader/lib/withStyles'
import s from './Child.css'

class Child extends React.Component {
  componentDidMount() {
    this.props.onRef(this)
  }
  componentWillUnmount() {
    this.props.onRef(undefined)
  }
  method() {
    window.alert('do stuff')
  }
  render() {
    return <h1 className={s.root}>Hello World!</h1>
  }
}

export default withStyles(s)(Child);
/* Parent.js */
import React from 'react'
import Child from './Child'

class Parent extends React.Component {
  onClick = () => {
    this.child.method() // do stuff
  }
  render() {
    return (
      <div>
        <Child onRef={ref => (this.child = ref)} />
        <button onClick={this.onClick}>Child.method()</button>
      </div>
    );
  }
}

Demo: https://jsfiddle.net/frenzzy/z9c46qtv/

145reactions
langpavelcommented, Mar 12, 2017

This is antipattern!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Call child method from parent - Stack Overflow
Create a boolean variable in the state in the parent class. Update this when you want to call a function. · Create a...
Read more >
Call child's function from a Parent component in React
Call child's function from a Parent component in React # · Wrap the Child component in a forwardRef . · Use the useImperativeHandle...
Read more >
How To Call A Child Function From A Parent Component In ...
Method #1 - Use the useImperativeHandle hook to call a child function · Declare the function you want to expose inside the useImperativeHandle ......
Read more >
How To Call Child Function From Parent Component In React
One way to call a child component's function from its parent is with the help of the useRef hook. Here's the gist of...
Read more >
Call Child Function from Parent Component in React
We can call methods or functions located inside a child component simply using references which is supported on class components and recently on ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found