ES6: fire React function

Tags: , ,



This is the code:

class Seismo extends Component {

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

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

What I am trying to do is to fire the updateNews code from render:

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

But keep getting this error:

Uncaught Error: this.updateNews is not a function

Answer

You were not calling the functuion

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

Note: You do need to bind because you use arrow function.



Source: stackoverflow