[英]Calling to super parent function from child component when it has nested multiple parents
Lets assume a React component is having multiple parents. 假设一个React组件有多个父对象。 When I wanted to call to a function of the supper parent(The highest level component). 当我想调用超级父级的函数时(最高级别的组件)。 How can I do that. 我怎样才能做到这一点。 Please see the sample code. 请参阅示例代码。
``` ```
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)} />
);
}
}
``` ```
Yeah. 是的 we can send the request via the props to the top of the hierarchy. 我们可以通过道具将请求发送到层次结构的顶部。 But is there any method we can do it in another way unless going through the hierarchy. 但是,是否有其他方法可以通过另一种方式完成此任务,除非遍历层次结构。 Thank You in advance. 先感谢您。
The react way is that you pass functionality from parent components down through props. 反应方式是您将功能从父组件向下传递到prop。 You mentioned this is your reply. 您提到这是您的回复。
Basically your parent would have a function which it passes down to a child which then triggers it. 基本上,您的父母将具有将其传递给孩子的功能,然后由孩子触发该功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.