Call React Parent component function from outside of the Child Component Class

有些话、适合烂在心里 提交于 2019-12-24 19:40:42

问题


Typically I do the following types of things from a Parent component

   this.childZ = React.createRef();  // 

   this.handleTabs = this.handleTabs.bind(this);  // bind a function 

   //Actual function in the parent class component 
   handleTabs = () => { ...}  // I want to call this from Child Component - but outside the class (i have 3rd party javascript etc..

   //In my render()   

   <Tab eventKey={19} title="CORE SEC Z."><SectionZ ref={(secZComponent) => {window.secZComponent.handleTabs() = secZComponent}} handleTabs={this.handleTabs} ref={this.childZ}/></Tab> 

Notice above I have SectionZ which is the child component.. I have function inside handleTabs={this.handleTabs} and I also have ref={(secZComponent) => {window.secZComponent.handleTabs() = secZComponent}} which I have used in the past to allow for some json rendered form data to be able to call a function

Seems that I cannot call parent from the child with either handleTabs={this.handleTabs} or window..... since it is outside the class component --> class SectionZ extends React.Component { }

来源:https://stackoverflow.com/questions/59166100/call-react-parent-component-function-from-outside-of-the-child-component-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!