export default class DetailsPage extends React.PureComponent {
constructor() {
super();
this.state = {
data: '',
isModalOpen: false,
};
**//this.openModal = this.openModal.bind(this);**
}
openModal() {
console.log(this, "this in Handler");
this.setState({ isModalOpen: true });
}
closeModal() {
this.setState({ isModalOpen: false });
}
Case 1 (No Binding in click Handler )
render() {
return (
(<div className="left">
<button className="btn btn btn-primary" type="button"
onClick={this.openModal}>Edit</button>
</div>
);
)
}
}
Case 2 (Binding with this)
render() {
return (
(<div className="left">
<button className="btn btn btn-primary" type="button"
onClick={this.openModal.bind(this)}>Edit</button>
</div>
);
)
}
}
In my click Handler openModal()
I have written console.log(this)
In Case 1 : the value of "THIS" comes out to be NULL
in react .
In Case 2 : the value of "THIS" comes out to be that of component (that is perfectly okay).
My doubts:
why does "THIS "comes out to be
NULL
in the first case.What is the use of
super()
in constructor ? I have read about it and got to know what happens if we remove thesuper()
and difference between super() & super(props). But I want to know the functionality that happens in the background when we callsuper()
.