Javascript 这就是在React中将父组件的方法传递给子组件的方式吗?

Javascript 这就是在React中将父组件的方法传递给子组件的方式吗?,javascript,reactjs,Javascript,Reactjs,在我正在编写的一本书的一节中,我解释了子组件如何访问父方法 从子级到父级的通信方式是将回调从父级传递给子级,子级可以调用回调来完成特定任务。在本例中,您将createIssue作为回调属性从IssueTable传递给IssueAdd。从子级,您只需在处理程序中调用传入函数来创建新问题 作者提到了IssueAddsibling的IssueAble兄弟姐妹,他可能是指IssueAddchild的IssueAlist父母,对吧 我认为只要检查IssueList返回的JSX 我们可以看到IssueTab

在我正在编写的一本书的一节中,我解释了子组件如何访问父方法

从子级到父级的通信方式是将回调从父级传递给子级,子级可以调用回调来完成特定任务。在本例中,您将createIssue作为回调属性从IssueTable传递给IssueAdd。从子级,您只需在处理程序中调用传入函数来创建新问题

作者提到了IssueAddsibling的IssueAble兄弟姐妹,他可能是指IssueAddchild的IssueAlist父母,对吧

我认为只要检查IssueList返回的JSX

我们可以看到IssueTable是IssueAdd的兄弟,不是吗

简而言之,要利用父函数中声明的函数,我们需要做的就是

const contentNode = document.getElementById('contents');


class ChildComponent extends React.Component {
  constructor(props) {
    super(props);
        this.props.someFunc; //So naming this prop someFunc will just help us identify this prop should get the function from the parent?
  }
}

class Parent extends React.component{
  constructor(props) {
    super(props);
        this.someFunc = this.someFunc.bind(this);
  }

  someFunc(){
   ....
  }

   render() {
    return (
      <div>
        <ChildComponent someFunc={this.someFunc} /> // Parent's someFunc gets passed as a value to the ChildComponent's prop which is someFunc?
      </div>
    );
  }
}

ReactDOM.render(<Parent />, contentNode);
是IssueTable和IssueAdd实际上是您发布的代码片段中的同级

class ChildComponent extends React.Component {
  constructor(props) {
    super(props);
        this.props.someFunc; //So naming this prop someFunc will just help us identify this prop should get the function from the parent?
  }
}
在上面的代码段中,this.props.someFunc将不起任何作用,它只返回从ParentComponent发送的函数,但不会发生任何事情

如果您计划通过ChildComponent中的操作修改或更改父级的状态,那么下面的代码片段可能更有意义

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

  handleOnClick = (event) => {
     // The name is someFunc because that is what you sent as props from the 
     // ParentComponent <ChildComponent someFunc={this.someFunc} />

     // If it had been <ChildComponent callbackToParent={this.someFunc} /> 
     // then you should access the function as this.props.callbackToParent 
     // and invoke as this.props.callbackToParent()

     this.props.someFunc();  
  }

  render() {
    return (
      <div onClick={this.handleOnClick}>
        Click to trigger callback sent by parent
      </div>
    ) 
  }
}

组件的布局方式,您发布的描述是错误的,您是正确的:createIssue回调从IssueList传递到IssueId。IssueList的子项是:IssueFilter、IssueTable和IssueId。
class ChildComponent extends React.Component {
  constructor(props) {
     super(props);         
   }

  handleOnClick = (event) => {
     // The name is someFunc because that is what you sent as props from the 
     // ParentComponent <ChildComponent someFunc={this.someFunc} />

     // If it had been <ChildComponent callbackToParent={this.someFunc} /> 
     // then you should access the function as this.props.callbackToParent 
     // and invoke as this.props.callbackToParent()

     this.props.someFunc();  
  }

  render() {
    return (
      <div onClick={this.handleOnClick}>
        Click to trigger callback sent by parent
      </div>
    ) 
  }
}