Want to add movies and push it into an array inside of Movie class. When I run it I get this warning:
index.js:2178 Warning: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/docs/forms.html#controlled-components in input (at index.js:41)
in label (at index.js:39)
in form (at index.js:38)
in Movie (at index.js:50)
class Movie extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.state = {list: []};//this line shows a waring
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.addMovie = this.addMovie.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('A movie was submitted: ' + this.state.value);
event.preventDefault();
this.addMovie();
}
addMovie(value){
this.setState({ list: [...this.state.list, value] });
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Movie name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
ReactDOM.render(
<Movie />,
document.getElementById('root')
);