react项目中有两个组件。1,父母 2,孩子
现在,我想在 Parent 组件中使用 childMethod 。在stackoverflow的一些页面中,大家都说refs是有效的。但在我的项目中,它不起作用。
class Parent extends Component {
parentMethod(){
this.refs.child.childMethod();
}
render() {
return (
<Child ref='child'/>
);
}
}
class Child extends Component {
childMethod() {
alert('You made it!');
}
render() {
return (
<h1 ref="hello">Hello</h1>
);
}
}
当我使用上述代码时,浏览器控制台中有一个错误代码。 _this3.refs.child.childMethod 不是函数
我想使用子方法,所以我有两个问题。1、什么是“_this3”?如何正确使用 refs?2、你有什么其他的想法吗?