【问题标题】:React - can't access passed functionReact - 无法访问传递的函数
【发布时间】:2018-11-27 09:16:52
【问题描述】:

我有一个表单,我使用一个组件来显示行,以便表单页面有 n 个组件页面。

      <form onSubmit={this.onSubmit} loading={loading} >
  <table className="ui celled table" border="1">
     <thead>
      <tr>
        <th >Status</th>
        <th >Date </th>
        <th >ID</th>
        <th >Plan</th>
      </tr>
    </thead>
    <tbody>
         {this.state.claims.map ( claim => <ClaimRow claim= { claim } updateClaims={this.updateClaims}  /> ) }
    </tbody>
    <tfoot className="full-width">
      <tr>
        <th colspan="4">
          <div className="ui small button">
            <Button Primary>Approve Selected</Button>
          </div>
          <div className="ui right floated button" >
              Next
                  <i class="right arrow icon"> </i>
          </div>
          <div className="ui right floated button" >
                  <i class="left arrow icon"></i>
             Prev
          </div>
        </th>
      </tr>
    </tfoot>
  </table>
  </form>

页面正确显示 ClaimRow 并且每一行都有一个 OnChange 事件处理程序。在 onChange 事件处理程序/行中,我想调用 updateClaims 来设置父组件中单个行的状态。这样,如果有人单击“批准”,则父组件的状态已准备好将所有更改发送到数据库。

当我单击“onChange”时,我的 onChange 处理程序中出现错误,表明它无法引用 updateClaims。

“TypeError: _this.updateClaims 不是函数”

我的 onChange 处理程序

  onChange = e => {
    this.setState({status: e.target.value} );
    this.updateClaims(this.state._id,this.state);

}

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    应该是

    this.props.updateClaims(this.state._id,this.state)

    在脚本中,

    &lt;ClaimRow claim= { claim } updateClaims={this.updateClaims} /&gt;

    updateClaims 作为一个 prop 传递给 ClaimRow 组件,它在其中的 props 上可用。

    要访问它,在类组件中,您需要在 this.props 上调用它

    【讨论】:

    • 正要说同样的事情,这个函数在哪里定义的,在parent中明确,child需要调用props(父数据)
    • 谢谢伙计们,我知道我错过了一些简单的东西。
    猜你喜欢
    • 1970-01-01
    • 2019-08-02
    • 2019-06-18
    • 1970-01-01
    • 2015-08-05
    • 2021-08-23
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多