Create a Controlled Input

Create a Controlled Input
0

#1

Tell us what’s happening:
Any suggestions on what the error is?

Thank you!

Your code so far


class ControlledInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: ''
    };
    // change code below this line
    this.handleChange = this.handleChange.bind(this);
    // change code above this line
  }
  // change code below this line
  handleChange(event){
      this.state.input = event.target.value;
  }
  // change code above this line
  render() {
    return (
      <div>
        { /* change code below this line */}
        <input value ={this.state.input} onChange={this.handleChange}/>

        { /* change code above this line */}
        <h4>Controlled Input:</h4>
        <p>{this.state.input}</p>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/create-a-controlled-input


#2

You need to use setState and not directly change state. The only time you should directly change state is in the constructor.


#3

Rather than directly assigning a value to input

Make sure to use this.setState when you are mutating states.