ES6:启动React功能

时间:2018-10-25 00:31:33

标签: javascript reactjs ecmascript-6

这是代码:

class Seismo extends Component {

  constructor(props) {
    super(props);
    this.state = {
      news: ""
    }
    this.updateNews = this.updateNews.bind(this)
  }

  updateNews = () => {
    console.log('test')
  }

我想做的是从updateNews触发render代码:

render() {
    return (
      <Button 
          type="primary"
          onClick={async () => {
              this.updateNews // This is what I am trying to fire!
          }
      >TEST</Button>

但请继续获取此错误:

  

未捕获的错误:this.updateNews不是函数

1 个答案:

答案 0 :(得分:2)

您没有打电话给功能

      <Button 
          type="primary"
          onClick={async () => {
              this.updateNews() // This is what I am trying to fire!
          }
      >TEST</Button>

注意: 您确实需要绑定,因为您使用了箭头功能。

相关问题