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
State
is an object, in order to create an object within a class I need to useconstructor
subclass's
constructor
will override the parent'sconstructor
, I don't know what is inReact.Component
but 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.
super
will help me do inherit, andsuper
is the replacement of the parent'sconstructor
. If I need to usethis
inconstructor
I need to writesuper
and to go further, I need to pass a parament dosuper(props)
to usethis.props
bind
will 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
props
constructor(props)
super(props)
) - The whole bind code looks odd to me,
this.doNothing
is 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.