What is the usage of Function.prototype.bind?
The bind
method returns a new function that is bound to a specific this
value or the "owner" object, So we can use it later in our code. The call
, apply
methods invokes the function immediately instead of returning a new function like the bind
method.
import React from 'react';class MyComponent extends React.Component {constructor(props) {super(props);this.state = {value: '',};// Binds the "handleChange" method to the "MyComponent" componentthis.handleChange = this.handleChange.bind(this);}handleChange(e) {//do something amazing here}render() {return (<><input type={this.props.type} value={this.state.value} onChange={this.handleChange} /></>);}}
October 12, 2022
759
Read more
What is the JavaScript += Operator and How Do You Use It?
December 04, 2022
JavaScriptUsing the startsWith() Method in JavaScript
December 04, 2022
JavaScriptReverse a String in JavaScript: 2 Easy Methods
December 02, 2022
JavaScript