bier hier bier hier -4 years ago 145
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 Source

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

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


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

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download