【问题标题】:React - Call Parent function from OUTSIDE of Child ComponentReact - 从子组件的外部调用父函数
【发布时间】:2020-03-28 21:07:18
【问题描述】:

我想从类函数外部获取子组件文件

函数 saveSurveyData(survey) 需要调用父函数handleTabs()

class Parent extends React.Component {

        constructor(props){
            super(props);       
            this.handleTabs = this.handleTabs.bind(this);
        }

         handleTabs = () => {
            console.log('expecting call from Child component - function 
         }


         render() {   
            return (
                <Tab eventKey={1} title="child">
                    <Child handleTabs={this.handleTabs} />
                </Tab> 

            )
        }
    }

// Child Component

import React from 'react';
    import * as Survey from "survey-react";

    var surveyValueChanged = function (sender, options) {
        saveSurveyData(survey);
    };
    // critical surveyjs function
    function saveSurveyData(survey) {
        var data = survey.data;

        //need to call handleTabs() (parent) in this function which is outside of Child Component Class
        //handleTabs={this.handleTabs}
        this.handleTabs();  // HOW ?
    }

    class Child extends React.Component {
        constructor(props){
            super(props);

        }

        render()
            {
                return(
                    <Survey.Survey model={survey}
                        onValueChanged={surveyValueChanged} />
                )
            }
                }

我一直在尝试 ref、props 和 state - 我只是不明白如何在子组件之外执行此操作。

【问题讨论】:

  • 如果handleTabs 仅具有通用的 Javascript 功能,您可以将其移出组件。如果它具有特定于 React 的功能(例如调用 setState),那么您必须将它从子函数传递给函数,并继续传递它,例如:onValueChanged={(sender, options) =&gt; surveyValueChanged(sender, options, this.props.handleTabs)} 等等,直到它到达需要的地方
  • handleTabs 确实有(需要)setState - 但是等等,看起来你说 this.props.handleTabs 会在孩子中设置......但是我如何从父母那里传下来- 你能给我一个答案吗/example thx
  • surveyValueChanged 被构建到一个surveyjs代码中,并且它在类组件之外 - 所以this.props ...它根本不喜欢

标签: javascript reactjs babeljs jsx


【解决方案1】:
<Child keyName={this.handleTabs} />

在父组件中,您将其作为道具传递(handleTabs 是父组件中的方法) 现在要访问 Child 组件中的任何 props 值,您可以直接使用(我已经更改了键名,以便您有清晰的概念)

//你想使用handleTabs(因为它是基于类的组件使用这个关键字)

this.props.KeyName();

您也不需要在父组件中绑定句柄选项卡,因为您创建了一个不需要绑定的箭头函数。 对于普通函数,你需要绑定 this。(所以绑定你的子函数)

import React from 'react';
import * as Survey from "survey-react";

class Child extends React.Component {
    constructor(props){
        super(props);
        this.saveSurveyData = this.saveSurveyData.bind(this);
        this.surveyValueChanged = this.surveyValueChanged.bind(this);
    }
surveyValueChanged(sender, options) {
    this.saveSurveyData(survey);
};

 saveSurveyData(survey) {
    var data = survey.data;
    this.props.handleTabs(); 
    console.log(this.props.handleTabs()) 
}



    render()
        {
            return(
                <Survey.Survey model={survey}
                    onValueChanged={this.surveyValueChanged} />
            )
        }
            }

【讨论】:

  • 我不明白这个。 - 我需要从子组件类外部调用父 handleTabs - keyName 是什么,然后 KeyName() 道具未在类组件外部定义...
  • @JeremyMiller 看到我想告诉您,无论您在 parent 中提供什么 keyName ,您都可以在 child 中从该名称访问。
  • 怎么样?当然我知道我可以访问子类组件内部,但是我的标题说“OUTSIDE”......因此你无法访问我已经测试过的反应道具
【解决方案2】:

handleTab 被传递给 this.props。您应该将surveyValueChanged 和saveSurveyData 放置到子组件中。

示例:

class Parent extends React.Component {

    constructor(props){
        super(props);       
        this.handleTabs = this.handleTabs.bind(this);
    }

     handleTabs = () => {
        console.log('expecting call from Child component - function 
     }


     render() {   
        return (
            <Tab eventKey={1} title="child">
                <Child handleTabs={this.handleTabs} />
            </Tab> 

        )
    }
}

/// 
// Child
import React from 'react';
import * as Survey from "survey-react";



class Child extends React.Component {
    constructor(props){
        super(props);

    }

    surveyValueChanged = (sender, options) => {
       this.saveSurveyData(survey);
    };
    // critical surveyjs function
    saveSurveyData = (survey) => {
       var data = survey.data;       
       this.props.handleTabs();
    }

    render()
        {
            return(
                <Survey.Survey model={survey}
                    onValueChanged={this.surveyValueChanged} />
            )
        }

}

【讨论】:

  • 但是你移动了函数——我知道这似乎无害,但是没有定义surveyValueChanged——我什至不知道将SurveyJS函数移动到React中的所有后果是否会真正起作用。
  • 你应该定义与上面相同的函数(使用箭头函数)或在构造函数中绑定它。
  • 好吧,我不情愿地将一堆surveyjs函数移到了类组件中。它现在正在工作。我以为我过去有问题 - 但似乎没问题。 - thx ,我仍然想知道我怎样才能作为某种类型的 javascript 原型获得访问权限.... 应该能够
猜你喜欢
  • 2017-03-11
  • 2015-11-09
  • 2020-02-15
  • 2019-07-27
  • 2017-04-15
  • 2016-09-07
  • 2020-03-25
  • 1970-01-01
相关资源
最近更新 更多