Javascript 从子组件类外部调用React父组件函数
通常,我从父组件执行以下类型的操作Javascript 从子组件类外部调用React父组件函数,javascript,reactjs,babeljs,jsx,Javascript,Reactjs,Babeljs,Jsx,通常,我从父组件执行以下类型的操作 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
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>
this.childZ=React.createRef();//
this.handleTabs=this.handleTabs.bind(this);//绑定函数
//父类组件中的实际函数
handleTabs=()=>{…}//我想从子组件调用它-但在类之外(我有第三方javascript等)。。
//在我的渲染()中
{window.secZComponent.handleTabs()=secZComponent}}handleTabs={this.handleTabs}ref={this.childZ}/>
注意上面我有SectionZ
,它是子组件..我有handleTabs={this.handleTabs}中的函数
我还有ref={(secZComponent)=>{window.secZComponent.handleTabs()=secZComponent}}
,我过去使用过它来允许一些json呈现的表单数据能够调用函数
似乎我无法使用handleTabs={this.handleTabs}或window从子级调用parent…,因为它位于
类组件-->
class SectionZ扩展React.component{}
尝试各种操作,例如调用window.handleTabs()的函数(子类组件外部)
但是上面说的是类型错误:window.handleTabs不是一个函数
我不清楚您试图实现什么。您试图在父组件中声明一个函数,但从子组件中调用它?@jered-是的。父组件包含所有选项卡,如果我在SecZ-child上,则有一个问题需要回答是的,那么我需要隐藏一个特定的选项卡(所有选项卡都在父组件上,父组件上的一个函数在页面加载时将被调用,并决定隐藏或显示该选项卡。因此,父函数也需要从子组件中调用。主要问题是,所有这些SurveyJS第三方代码都在my Child react组件类之外。如果您可以自定义,您可以使用React.cloneElement
将您想要的任何内容传递给子组件,并将其作为道具接收。@keikai您有这样做的示例吗?