Meteor / React - Material UI组件芯片无法识别onRequestDelete函数

时间:2016-07-07 23:51:20

标签: javascript meteor reactjs material-ui

我在我的应用程序中使用Material UI的芯片元素并根据文档

onRequestDelete - Callback function fired when the delete icon is clicked. If set, the delete icon will be shown.

import React from 'react'
import { createContainer } from 'meteor/react-meteor-data'
import Chip from 'material-ui/Chip'

class InfluencerChips extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      currentInfluencers: [{name: {first: 'Test', last: 'User'}, relationship: 'Friend'}]
    }

    this.handleDelete = this.handleDelete.bind(this)
  }

  handleDelete(id){
    console.log(id)
  }

  render() {
    return(
      <div>
        { this.state.currentInfluencers.map((influencer, k) =>
          <Chip
            key={k}
            onRequestDelete={this.handleDelete(influencer.id)}
          >
            {influencer.name.first}
          </Chip>
        )}

      </div>
    )
  }
}

我似乎无法获得删除&#39; handleDelete&#39;功能被识别为功能。它呈现了这个:

enter image description here

但是当我改变这一行:

onRequestDelete={function(){}}

使用像这样的关闭图标正确渲染

enter image description here

那么handleDelete(id)实际上是一个函数吗? React的处理方式是什么?提前致谢

1 个答案:

答案 0 :(得分:2)

因为

this.handleDelete(influencer.id)不是函数,它是函数返回的返回值 this.handleDelete是一个函数,但您需要值而不是事件

将其更改为:

onRequestDelete={() => this.handleDelete(influencer.id)}