为什么我不能在ReactJS中从“外部”访问组件方法?为什么不可能?有没有办法解决这个问题?
考虑下面的代码:
var Parent = React.createClass({
render: function() {
var child = <Child />;
return (
<div>
{child.someMethod()} // expect "bar", got a "not a function" error.
</div>
);
}
});
var Child = React.createClass({
render: function() {
return (
<div>
foo
</div>
);
},
someMethod: function() {
return 'bar';
}
});
React.renderComponent(<Parent />, document.body);
React通过ref属性提供了一个接口,用于您尝试执行的操作。为一个组件分配一个ref,它的当前属性将是你的自定义组件:
class Parent extends React.Class {
constructor(props) {
this._child = React.createRef();
}
componentDidMount() {
console.log(this._child.current.someMethod()); // Prints 'bar'
}
render() {
return (
<div>
<Child ref={this._child} />
</div>
);
}
}
注意:只有当子组件被声明为一个类时,这才会起作用,参见这里的文档:https://facebook.github.io/react/docs/refs-and-the-dom.html#adding-a-ref-to-a-class-component
更新2019-04-01:更改示例,每个最新的React文档使用一个类和createRef。
更新2016-09-19:修改示例,从ref String属性文档中使用ref回调。
正如在一些评论中提到的,ReactDOM。渲染不再返回组件实例。你可以在呈现组件的根时传入一个ref回调来获取实例,如下所示:
// React code (jsx)
function MyWidget(el, refCb) {
ReactDOM.render(<MyComponent ref={refCb} />, el);
}
export default MyWidget;
and:
// vanilla javascript code
var global_widget_instance;
MyApp.MyWidget(document.getElementById('my_container'), function(widget) {
global_widget_instance = widget;
});
global_widget_instance.myCoolMethod();
另一种方法很简单:
函数外:
function funx(functionEvents, params) {
console.log("events of funx function: ", functionEvents);
console.log("this of component: ", this);
console.log("params: ", params);
thisFunction.persist();
}
绑定:
constructor(props) {
super(props);
this.state = {};
this.funxBinded = funx.bind(this);
}
}
请在这里查看完整的教程:如何从外部使用React组件的“this”?