I'm starting with react and the first thing I did is to go to the main page where I see some examples, e.g.:
class MarkdownEditor extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.state = { value: 'Type some *markdown* here!' };
}
handleChange(e) {
this.setState({ value: e.target.value });
}
getRawMarkup() {
const md = new Remarkable();
return { __html: md.render(this.state.value) };
}
render() {
return (
<div className="MarkdownEditor">
<h3>Input</h3>
<textarea
onChange={this.handleChange}
defaultValue={this.state.value}
/>
<h3>Output</h3>
<div
className="content"
dangerouslySetInnerHTML={this.getRawMarkup()}
/>
</div>
);
}
}
ReactDOM.render(<MarkdownEditor />, mountNode);
... I see that in the constructor is done:
this.handleChange = this.handleChange.bind(this);
What is the reason behind that? Talking with colleagues they think some possible reasons like something special behind the render() method, or something related with non supported feature of ES6.
Do someone have a specific explainment to that?
In a first look, it seems unnecessary for me as shown in this simple example:
class A {
constructor(){
this.a = 1;
}
do(){
console.log('A', this.a);
}
}
class B extends A {
constructor(){
super();
this.a = 2;
}
do(){
console.log('B', this.a);
}
}
console.log ((new B()).do())