Recently began to teach React on video lessons. I tried to write all sorts of "things" on it and somehow came across the official documentation translated into Russian, with this example:
class Form extends React.Component { constructor(props) { super(props); this.state = {value: ''}; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { this.setState({value: event.target.value}); } handleSubmit(event) { alert('Text field value is: ' + this.state.value); } render() { return ( <div> <input type="text" placeholder="Hello!" value={this.state.value} onChange={this.handleChange} /> <button onClick={this.handleSubmit}> Submit </button> </div> ); } } ReactDOM.render( <Form />, document.getElementById('root') ); In the video lessons for which I studied I was a little wrong, namely:
- What is the difference from creating a component by inheriting class Form extends React.Component from ordinary React.createClass ()?
- Why binds this in the class constructor with bind, if it works without it?
- What is the general advantage of the code from the documentation over my code? (below)
var Form = React.createClass({ getInitialState: function(){ return { value: "" } }, handleChange(e){ this.setState({value: e.target.value}); }, handleSubmit(e){ alert("Text field value is: " + this.state.value); }, render: function(){ return ( <div> <input type="text" placeholder="Hello!" value={this.state.value} onChange={this.handleChange} /> <button onClick={this.handleSubmit}> Submit </button> </div> ); } }); ReactDOM.render( <Form />, document.getElementById('root') ); Thanks in advance: 3