When a method is being bound in the React class constructor, how does it view the value of the property being bound, if the constructor is the first thing to run, then when the handleClick property is being bound to the component instance below, how does it know what the value of handleClick will be as it's not defined until after the constructor.
So I'm wondering if the constructor runs first, is it even aware that a method called handleClick has been defined and if so how because it's defined after the constructor?
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
);
}
}