O'Reilly logo

React and React Native by Adam Boduch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Inline event handlers

The typical approach to assigning handler functions to JSX properties is to use a named function. However, sometimes we might want to use an inline function. This is done by assigning an arrow function directly to the event property in the JSX markup:

import React, { Component } from 'react'; 
 
export default class MyButton extends Component { 
 
  // Renders a button element with an "onClick()" handler. 
  // This function is declared inline with the JSX, and is 
  // useful in scenarios where you need to call another 
  // function. 
  render() { 
    return ( 
      <button 
        onClick={e => console.log('clicked', e)} 
      > 
        {this.props.children} 
      </button> 
    ); 
  } 
} 

The main use of inlining event handlers like this is when you have a static parameter value that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required