首先我不清楚你的这种需求是怎么产生的。
多个组件调用A组件的某个方法fn,这种完全可以根据组件通信去解决,不建议使用这种“黑科技”
如果只是学术上的问题 如何拿到组件的实例
那么很简单,react提供了一种方法就是this.refs api
比如// this.refs.div
这个api已经废弃了,现在的写法是这样的// this.div 现在就是div的引用了
this.div = ref} />
在最新的react16.3中api进行了再次升级,现在的写法是这样的。class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
// const node = this.myRef.current; 这样可以拿到组件实例
return
}
}
希望我的回答对你有帮助。