0
React 구성 요소에 여러 부모가 있다고 가정합니다. 저녁 부모 (최고 수준의 구성 요소)의 기능을 호출하고 싶을 때. 어떻게해야합니까? 샘플 코드를 참조하십시오.여러 부모가 중첩되어있을 때 하위 구성 요소의 수퍼 부모 함수 호출
```
export default class WeekView extends React.Component {
constructor(props) {
super(props);
}
// this functions has to be called from third child
loadData() {
var data = [];
this.setState({events : data});
}
render() {
return (
<ChildOne >
);
}
}
export class ChildOne extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<ChildTwo />
);
}
}
export class ChildTwo extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<ChildThree />
);
}
}
export class ChildThree extends React.Component {
constructor(props) {
super(props);
}
addEvent() {
// how to call loadData() function, which is included in the highest parent
}
render() {
return(
<Button onCLick={this.addEvent.bind(this)} />
);
}
}
```
그래. 우리는 소품을 통해 요청을 계층 구조 맨 위로 보낼 수 있습니다. 그러나 계층 구조를 거치지 않는 한 다른 방법으로이를 수행 할 수있는 방법이 있습니까? 미리 감사드립니다.
이 사실을 알게 된 후 #fllux 아키텍처가이 문제를 해결하는 데 도움이된다는 것을 알아야합니다. –
다음은 [builtwithreact.com] (http://buildwithreact.com/tutorial/events)에서 채택 된 개념의 [jsbin] (http://jsbin.com/yayatucowu/1/embed?js,output)입니다. – cjsimon