How to avoid binding by using arrow functions in callbacks in ReactJS? Last Updated : 18 Feb, 2022 Comments Improve Suggest changes Like Article Like Report In React class-based components when we use event handler callbacks, it is very important to give special attention to the 'this' keyword. In these cases the context this is undefined when the callback function actually gets invoked that's why we have to bind the context of this. Now if binding all the methods of each class is very annoying. Instead of binding we can use the inline arrow function since the arrow function does not have its own value of this, it uses the parent or public value. Using the inline arrow function we can get rid of annoying method binding every time and also the code looks very packed and organized. Example 1: This example illustrates how to use arrow functions in callbacks index.js: JavaScript import React from 'react' import ReactDOM from 'react-dom' import App from './App' ReactDOM.render(<App />, document.querySelector('#root')) App.js : JavaScript import React, { Component } from 'react' class App extends Component { // Default props static defaultProps = { courseContent : [ 'JSX', 'React Props', 'React State', 'React Lifecycle Methods', 'React Event Handlers', 'React Router', 'React Hooks', 'Readux', 'React Context' ] } constructor(props){ super(props) // Set initial state this.state = {msg : 'React Course', content:''} } // Return an unordered list of contents renderContent(){ return ( <ul> {/* map over all the contents and return some JSX for each */} {this.props.courseContent.map(content => ( <li>{content}</li> ))} </ul> ) } render(){ const button = !this.state.content && <button // Arrow function in callback onClick={() => { // Update state this.setState({ msg : 'Course Content', content : this.renderContent() }) }} > Click here to know contents! </button> return ( <div> <p>{this.state.msg}</p> <p>{this.state.content}</p> {button} </div> ) } } export default App Output : Example 2 : index.js : JavaScript import React from 'react' import ReactDOM from 'react-dom' import App from './App' ReactDOM.render(<App />, document.querySelector('#root')) App.js : JavaScript import React, { Component } from 'react' class App extends Component{ // Default props static defaultProps = { name : ['John', 'Alex', 'Bob'] } constructor(props){ super(props) // Nnitialize count state this.state = {msg : 'Hi There', count:0} } render(){ return( <div> <h3>Greetings!</h3> <p>{this.state.msg}</p> <button // Arrow function in callback // does not required explicit binding onClick={() => { this.setState(st => ( st.msg = `${st.msg}, ${this.props.name[st.count]}`, st.count += 1 )) }} > Say greeting to employees! </button> </div> ) } } export default App Output : Create Quiz Comment H hunter__js Follow 0 Improve H hunter__js Follow 0 Improve Article Tags : Web Technologies ReactJS Explore React FundamentalsReact Introduction6 min readReact Environment Setup3 min readReact JS ReactDOM2 min readReact JSX5 min readReactJS Rendering Elements3 min readReact Lists4 min readReact Forms4 min readReactJS Keys4 min readComponents in ReactReact Components4 min readReactJS Functional Components4 min readReact Class Components3 min readReactJS Pure Components4 min readReactJS Container and Presentational Pattern in Components2 min readReactJS PropTypes5 min readReact Lifecycle7 min readReact HooksReact Hooks8 min readReact useState Hook5 min readReactJS useEffect Hook5 min readRouting in ReactReact Router5 min readReact JS Types of Routers10 min read Advanced React ConceptsLazy Loading in React and How to Implement it ?4 min readReactJS Higher-Order Components5 min readCode Splitting in React4 min readReact ProjectsCreate ToDo App using ReactJS3 min readCreate a Quiz App using ReactJS4 min readCreate a Coin Flipping App using ReactJS3 min readHow to create a Color-Box App using ReactJS?4 min readDice Rolling App using ReactJS4 min readGuess the number with React3 min read Like