Skip to content
Advertisement

ES6: fire React function

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

Advertisement

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement