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's method or child's refs?

See original GitHub issue

I want to call child method or reference to child’s refs. It should work, but refs.child returns ProxyComponent rather than Child. And, the ProxyComponent does not have method nor refs.root. I think this is due to having hot module reloading together with withStyles. Any ideas? I’ve tried with release mode as well, but to no avail.

Child.js

import React, {PropTypes} from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Child.css';


class Child extends React.Component {
  method() { console.log('child'); }

  render() {
    return (
      <div ref="root">
      </div>
    );
  }
}

export default withStyles(s)(Child);

Parent.js

import React, {PropTypes} from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Parent.css';
import Child from './Child';


class Parent extends React.Component {
  callChildMethod() { this.refs.child.method(); } // doesnt work.

  render() {
    return (
      <div>
        <Child ref="child" />
      </div>
    );
  }
}

export default withStyles(s)(Parent);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
frenzzycommented, Feb 8, 2017

This approach works fine with hot reload in master branch. Maybe you want to use this and just forgot to bind context?

P.S.: Added a tiny new section to React docs: When to Use Refs

2reactions
frenzzycommented, Feb 7, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to call a child function using refs in Vue.js
In this tutorial, you'll learn how to directly access child functions on parent events in Vue.js using the ref attribute. There are other...
Read more >
vue.js - call child method from parent without ref in VUE3
I want to dymanic grab a new grid-item into the grid-layout. the library offer the way to grab from outside,but I face a...
Read more >
How to call the child component function from the parent ...
When we enclose the child component with forwardRef , it receives a second parameter apart from props, which is the ref passed from...
Read more >
How to call a child function using refs in Vue.js
In this tutorial, you'll learn how to directly access child functions on parent events in Vue.js using the ref attribute.
Read more >
Call function of child from parent good practices - Vue Forum
When a parent needs to call a child's method I would simply use a ref . Using a prop and watcher to trigger...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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