简体   繁体   English

如何访问 function 作为在父组件中定义的子组件中的道具?

[英]How can i access function as a prop in child component that define in parent component?

I am trying to use a function as a prop in child component(DetailViewOfEntity) that is defined in the parent component(App.js) .我正在尝试使用 function 作为父component(App.js)中定义的子component(DetailViewOfEntity)中的道具。 When I click on the button,I am getting an error that this.setState and obj.detailview is not defined.当我单击按钮时,我收到一个错误,即this.setStateobj.detailview未定义。 Please also look at the image attached below for the exact error that I am getting.另请查看下面附上的图片,了解我得到的确切错误。

Thank you谢谢

App.js component App.js 组件

import  React from "react";

import DetailViewOfEntity from "./DetailViewOfEntity";


export default class App extends React.Component {
  constructor(props, context) {
    super(props, context);

    this.viewEntity = this.ViewEntity.bind(this);

    this.state = {

        viewEntityState: false,
    
    };

  }


  }
  
  ViewEntity (para) {
    var obj = this;
    this.setState ({
      viewEntityState: true,
    });                                      

    obj.DetailView();//another function
  }

  DetailView() {
    console.log("we are in detail view function");
  }


  render() {

    return (
      <div>
        
        <DetailViewOfEntity 
            test = {this.ViewEntity}
        /> 
      </div>
    );
  }
}

DetailView Component详细视图组件

import React from "react";

export default class DetailViewOfEntity extends React.Component {
    constructor (props, context) {
        super (props, context);
    }

    UpdateCommentsFromCRM() {

        this.props.test(); //when i call this function, i got an error.
    }
  render () { 
      return (
          <div>

            <button onClick = {() => this.UpdateCommentsFromCRM()}>
            </button>
                     
          </div>
      );
  }
}

enter image description here在此处输入图像描述

your problem is that you are calling:你的问题是你在打电话:
this.viewEntity = this.ViewEntity.bind(this); instead of this.ViewEntity = this.ViewEntity.bind(this);而不是this.ViewEntity = this.ViewEntity.bind(this);

as @kapil pandey pointed out and here I share with you the working version on code sandbox正如@kapil pandey 指出的那样,在这里我与您分享代码沙箱上的工作版本

codesandbox demo 代码沙盒演示

It's giving error because of function binding.由于 function 绑定,它给出了错误。 If you using 'create-react-app' , you can omit the constructor and use arrow function .如果您使用'create-react-app' ,则可以省略构造函数并使用箭头 function Hopefully, you won't give any error.希望你不会给出任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从父组件 prop 访问子组件 prop - How to access the child component prop from parent component prop 如何在子组件内部调用父组件的功能? - How can I call the function of a parent component inside of a child component? 当它作为道具传递时,如何将状态从父组件更新到子组件? - How can I update the state from parent component to child component when its passed as a prop? 在 REACT.js 中,如何将状态从子组件传递到父组件作为道具 - In REACT.js how can I pass a state from child component up to Parent component as a prop 从子组件访问父道具/属性 - Access parent prop / attribute from child component 如何访问子组件的子组件中的道具 - How to access prop in child of child component 我如何绑定到已通过prop传递给子组件的函数,以便可以在子组件的函数中调用它 - How do I bind to a function that has been passed in to a child component via a prop so that I can call it in a function in the child component 如何从 React 中的父组件调用子 function - How can I call Child function from Parent Component in React ReactJS:如何从子组件访问道具 - ReactJS: how to access prop from child component 开玩笑如何从作为道具传递给子组件的父组件调用 function - Jest how to call a function from parent component that is passed as a prop to child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM