What are arrow functions in ReactJS?

Arrow functions are an alternative way to define JavaScript functions. They provide a more compact function expression and reduce the programmer's keystrokes.

Note: Click here to learn about arrow functions in JavaScript.

In React, we use arrow functions as functional components, functions within class components, and event handlers.


Functional components

We can use arrow functions to create functional components in React, just like regular functions.

Note: Click here to learn about React's functional components.

const App = () => {
  const user = 'Guest';

  return (
    <Message user={user} />
  );
}

const Message = ({user}) => {
  return <h1>Welcome {user}!</h1>
}

export default App;
A functional component defined as an arrow function

We can make the code much cleaner for smaller components with only a single line of code.

const App = () => {
  const user = 'Guest';

  return (
    <Message user={user} />
  );
}

const Message = ({user}) => <h1>Welcome {user}!</h1>

export default App;
A one-line functional component

We no longer have to write the {} braces or the return statement.

Functions in class components

Using normal functions inside class components requires us to bindMaking a function more specific in its usage. them to the component. By default, the this keyword in regular functions refers to the object calling the function. Hence, this can refer to different objects in different situations. We can bind regular functions using the bind() method to access the component's attributes using the this keyword.

import {Component} from 'react';

const App = () => {
  const user = 'Guest';

  return (
    <Message user={user} />
  );
}

class Message extends Component {
  constructor(props) {
    super(props);
    this.state = {display: false};
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    this.setState({display: !this.state.display});
  }
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click me!</button>
        {this.state.display && <h1>Welcome {this.props.user}!</h1>}
      </div>
    );
  }
};

export default App;
Using the 'bind()' method in constructor to allow handleClick() to use 'this' keyword

Explanation

This example considers a simple web application that displays a welcome message when users click the button.

  • Line 7: We return the Message component and pass it the name of the user we want to display. In this case, we want to display 'Guest'.
  • Line 11: We define the class component, Message.
  • Line 12–14: We define the constructor and create a state with one boolean property.
  • Line 15: We bind the handleClick() function. Doing so allows us to use the this keyword without errors within the scope of handleClick().
  • Line 17–19: We define the handleClick() function. It toggles the display property between true and false when it is called.
  • Line 23: We create a button. When users click it, the handleClick() function is called.
  • Line 24: We display the message if the display property is true.

With arrow functions, we can circumvent the need for binding. This is because the this keyword in arrow functions refers to the object that defined the arrow function. When we define an arrow function within a class, this will always refer to that class.

We can avoid explicit binding in one of two ways.

Defining the function as an arrow function

If we create the function as an arrow function, it automatically uses this to refer to the class. In other words, we don't need to bind an arrow function.

import {Component} from 'react';

const App = () => {
  const user = 'Guest';

  return (
    <Message user={user} />
  );
}

class Message extends Component{
  constructor(props) {
    super(props);
    this.state = {display: false};
  }
  handleClick = () => this.setState({display: !this.state.display});
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click me!</button>
        {this.state.display && <h1>Welcome {this.props.user}!</h1>}
      </div>
    );
  }
};

export default App;
An arrow function created within a class component

On line 16, we create the handleClick() function as an arrow function, hence we don't need to bind it.

Using an arrow function in the render()function

Alternatively, we can define the function normally but call it within render() using an arrow function. Doing so makes the this keyword refer to the class.

import {Component} from 'react';

const App = () => {
  const user = 'Guest';

  return (
    <Message user={user} />
  );
}

class Message extends Component{
  constructor(props) {
    super(props);
    this.state = {display: false};
  }
  handleClick() {
    this.setState({display: !this.state.display});
  }
  render() {
    return (
      <div>
        <button onClick={() => this.handleClick()}>Click me!</button>
        {this.state.display && <h1>Welcome {this.props.user}!</h1>}
      </div>
    );
  }
};

export default App;
Calling a regular function using an arrow function within render()

In the above example, we define the handleClick() function normally.

On line 22, we call the handleClick() function using an arrow function.

Note: Creating arrow functions in the render() function creates a new function every time the component is rendered. For optimization concerns, the above method should be avoided when possible.

Event handlers

We can use the regular function calls within the render() function (for class components) or return statement (for functional components) to produce the 'too many re-renders' error. This error means that the component is being rendered again and again, infinitely.

import {Component, useState} from 'react';

const App = () => {
  const user = 'Guest';
  return (
    <div>
      <Message user={user} />
    </div>
  );
}

const Message = ({user}) => {
  const [display, setDisplay] = useState(false);
  return (
    <div>
      <button onClick={setDisplay(!display)}>Click me!</button>
      {display && <h1>Welcome {user}!</h1>}
    </div>
  );
};

export default App;
Calling a function normally within the return statement

The terminal will not show any errors, but the output is a blank page. For closer inspection, we can open the browser's console (through inspect element or other means depending on the browser). There we can see the following error:

The 'Too many re-renders' error displayed on a browser's console

We can avoid this error by using an arrow function.

import {Component, useState} from 'react';

const App = () => {
  const user = 'Guest';
  return (
    <div>
      <Message user={user} />
    </div>
  );
}

const Message = ({user}) => {
  const [display, setDisplay] = useState(false);
  return (
    <div>
      <button onClick={() => setDisplay(!display)}>Click me!</button>
      {display && <h1>Welcome {user}!</h1>}
    </div>
  );
};

export default App;
Call a function via an arrow function within the return statement

An arrow function prevents the setDisplay() function from executing after the component is mounted. It onlys execute when users click the button.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved