I looked up what does constructor, super and bind does in General JS.
Example code.
import React from 'react';
class Example extends React.Component {
constructor(props){
super(props);
this.state = { mood: confused }
this.doNothing = this.doNothing.bind(this);
}
doNothing(){} //I really do nothing
render(){
<button onClick={this.doNothing}>I do nothing xD</button>
}
}
Here is what I understand for now
Stateis an object, in order to create an object within a class I need to useconstructorsubclass's
constructorwill override the parent'sconstructor, I don't know what is inReact.Componentbut I am sure it is important. I think it is also said in React Document:Class components should always call the base constructor with props.
superwill help me do inherit, andsuperis the replacement of the parent'sconstructor. If I need to usethisinconstructorI need to writesuperand to go further, I need to pass a parament dosuper(props)to usethis.propsbindwill create a new function that is bounded well with the object, making sure when the function is called, it will be direct to the right object because for some reason if I don't bind it, my button will be like<button onClick={undefined.doNothing}>, because of some class rule :/ (the optional paraments can also help me set pre arguments which are interesting but that isn't what bothers me)
Here is what I don't understand
- The meaning of arguments I passed, I have seen some example but they didn't really pass any argument. (The
propsconstructor(props)super(props)) - The whole bind code looks odd to me,
this.doNothingis changed into what?this.this.doNothing? How does it really works, why my button knows where to find it?
I know this is kinda basic but I did try my best looking things up, I will appreciate if anyone can help me out.