bier hier bier hier - 21 days ago 6
React JSX Question

How can I get arrow functions working?

Getting my head around ES6 and was wondering how to create an arrow function which you can debug:

<button onClick={() => this.setState({open: !open})}>Toggle</button>


How can I put a console.log in here to see whether this has been triggered or not?

Answer

Use Braces{} for a arrow function which will have a block of code.

<button onClick={() => {console.log('hello');this.setState({open: !open})}}>Toggle</button>

Formatted

 <button onClick={() => {
       console.log('hello');
       this.setState({open: !open})
    }}>
      Toggle
    </button>

Hope this helps!

Comments