React调用其它组件的方法
跳到导航
跳到搜索
一个react组件实际上是一个类。所以只要把它new出来就可以了!
let bComponent=new BComponent(this.props);
bComponent.testMethod();
但是要注意,我在实际使用时,发现A组件调用B组件的方法时,B组件的this是指向A组件的。所以B组件中的方法如果操作了一些props,states,要确保A组件也有相同的props和states。
还有,如果被调用的组件是个高阶组件,比如它被redux的connect方法包装过,要使用WrappedComponent。
let bComponent=new BComponent.WrappedComponent(this.props);
bComponent.testMethod();
好吧,这个插件不支持ES6语法。。。