简体   繁体   English

当子组件嵌套了多个父组件时,从子组件调用超级父函数

[英]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.

相关问题 Javascript:从父(超级?)函数调用子函数。 - Javascript: Calling child function from parent (super?) function. 子调用具有http请求的父函数后,如何在子组件中更新父变量 - How to update parent's variable in child component after child has called parents function that has http request 在 React 中从父组件到子组件调用 function - Calling function from parent to child component in React 如何从父表单组件获取数据到子组件嵌套组件数据从父到子 - How to get data from parent form component to child component nested components data from parents to children 当子组件功能完成时,Vue调用父组件功能 - Vue calling parent component function when child component function completes 使用NextJS从页面(子)组件调用布局(父)组件功能 - Calling layout (parent) component function from page (child) component with NextJS 在React.js中从父组件调用子组件函数 - Calling Child component function from Parent component in React.js Aura.js Lightning组件:从上级/上级组件触发嵌套/子组件的方法吗? - Aura.js Lightning Components: Firing a nested/child component's methods from the super/parent component? 加载父组件数据后,如何从子组件调用函数(Angular 5) - How to call function from child component when parent component data has loaded (Angular 5) React Native:从子级调用父级组件的函数时遇到问题 - React Native: Having an issue calling a function of a parent component from a child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM