How to call child's method or child's refs?
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This approach works fine with hot reload in master branch. Maybe you want to use
this
and just forgot tobind
context?P.S.: Added a tiny new section to React docs: When to Use Refs
Like so?