React - Create a Controlled Input

Tell us what’s happening:
Describe your issue in detail here.

Not sure why my code isn’t working. I checked the hints just to see what I was doing wrong and the code matches from what I see. Is there maybe a typo somewhere I didn’t catch? It’s passing the first two tests, but not the last one: “Typing in the input element should update the state and the value of the input, and the p element should render this state as you type.”

Your code so far

class ControlledInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: ''
    };
    // Change code below this line
this.handleChange.bind(this);
    // Change code above this line
  }
  // Change code below this line
handleChange(event) {
  this.setState({
    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 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0

Challenge: React - Create a Controlled Input

Link to the challenge:

Check the hint session again ?

I think I see what is missing. I didn’t initialize it. Thanks for helping me catch that.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.