0
votes

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')
);
2

2 Answers

1
votes
this.state = {value: ''};
this.state = {list: []};

Your code is saying that first I want my state object to have a state with property value. Then you reassign a new state object with key list and replace the old state. This is wrong, you should define multiple properties in a single state object as below

this.state = {value: '', list: []}
1
votes

You can't have multiple this.state in constructor

  constructor(props) {
    super(props);
    this.state = {value: '',list: []};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.addMovie = this.addMovie.bind(this);

  }

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: '', list: [] };
  

    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() {
    let value = this.state.value;
    this.setState({ list: [...this.state.list, value],value: '' });
  }
  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
      
          Movie name: 
          <ul>
            {this.state.list !== [] && this.state.list.map((row, i) => <li key={i} >{row}</li>)}
          </ul>
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}


ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root'></div>