0
Parent
에서 렌더링 된 Child
구성 요소의 메소드를 어떻게 호출 할 수 있습니까? 기본적으로 내 응용 프로그램은 switch
문을 사용하여 각각 루트에 MyComponent
요소를 포함하는 3 개의 탭을 렌더링합니다. props.children
을 사용하여 액세스를 시도했지만 자식 메서드를 호출하면 오류가 발생합니다.React : props.children의 구성 요소를 얻으십시오
/* parent */
changeChild(component) {
this.setState({component});
}
callChildMethod() {
this.state.component.childMethod();
}
render() {
return <Child tab={1} changeChild={this.changeChild} />
}
/* child */
componentDidMount() {
this.props.changeChild(this.refs.root.props.children) // pass to parent here
}
renderContent() {
switch(this.props.tab) {
case 0:
return (
<MyComponent />
);
case 1:
return (
<MyComponent />
);
case 2:
return (
<MyComponent />
);
}
}
render() {
return (
<div ref="root">
{this.renderContent()}
</div>
)
}
의 메소드를 호출하고 싶습니다. 정상적으로 작동합니다. – bogdanpetru