The bind() is an inbuilt method in React that is used to pass the data as an argument to the function of a class based component.
Syntax:
this.function.bind(this,[arg1...]);
Parameter:
It accepts two parameters, the first parameter is the
- this: keyword used for binding
- [arg1...]: the sequence of arguments that are passed as a parameter and are optional.
Steps to Create React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:

Example 1: This example uses the bind method to pass the argument "GeeksForGeeks" to the handler function while ensuring it still has the correct this context.
JavaScript
// Filename - App.js
import React from "react";
class App extends React.Component {
// Initialising state
state = {
name: "GFG"
};
handler = (name) => {
// Changing the state
this.setState({ name: name });
};
render() {
return (
<div>
<h1>Name:{this.state.name}</h1>
<h1>Click here to change the name</h1>
{/* Passing the name as an argument
to the handler() function */}
<button onClick={this.handler.bind(this, "GeeksForGeeks")}>
Click Here
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
The forceUpdate()
method is useful for forcing a component to re-render.
npm start
Output:
Example 2: This example uses the modern ES6 arrow function in the onClick event to directly pass the argument "GeeksForGeeks" to the handler function, eliminating the need for the bind method.
JavaScript
// Filename - App.js
import React from "react";
class App extends React.Component {
// Initialising state
state = {
name: "GFG"
};
handler = (name) => {
// Changing the state
this.setState({ name: name });
};
render() {
return (
<div>
<h1>Name:{this.state.name}</h1>
<h1>Click here to change the name</h1>
{/* Passing the name as an argument
to the handler() function
with modern ES6 feature*/}
<button onClick={() => this.handler("GeeksForGeeks")}>
Click Here
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Similar Reads
ReactJS Data Binding Data binding is a technique that binds data sources from the provider and consumer together and synchronizes them. In other words, Data Binding means sharing data between components to view and view to component.Data binding in React can be achieved by using a controlled input. A controlled input is
7 min read
ReactJS constructor() Method In React, lifecycle methods help control what happens at different stages of a component's life. The constructor() method is one of the first methods called when a class component is created. It is mainly used to set up the initial state and bind event handlers before the component appears on the sc
4 min read
ReactJS Methods as Props In this article, we will learn about props and passing methods as props. We will also discuss how we can use the child components to pass data to parent components using methods as props.What are props?We know that everything in ReactJS is a component and to pass in data to these components, props a
3 min read
ReactJS render() Method In React, lifecycle methods manage a componentâs behaviour at different stages. The render() method is important for defining the UI, updating it whenever state, props, or context changes, and ensuring the UI stays in sync with the componentâs data.What is the Render() Method in ReactJS?The render()
6 min read
ReactJS | Binding Event Handlers In ReactJS, we need to bind events so that the this keyword would not return an "undefined". In this article, we are going to see the different ways in which we can bind event handlers in ReactJS. First, let's make a new class component and name it Binding.js. Make a simple class component with a si
3 min read