React - Create a Controlled Form

Tell us what’s happening:
Following is my code but I do not know why I cannot pass all the tests, It is failing on

Typing in the input element should update the input property of the component’s state.

Not sure what is wrong, spend a long time on this but now exhuasted.

Your code so far

class MyForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '',
      submit: ''
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleChange(event) {
    this.setState({
      input: event.target.value
    });
  }
  handleSubmit(event) {
    // Change code below this line
    event.preventDefault();
    this.setState({
      submit: this.state.input
    })
    // Change code above this line
  }
  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          {/* Change code below this line */}
          <h1>{this.state.submit}</h1>
<input type="text" onChange={this.handleChange} />
          {/* Change code above this line */}
          <button type='submit'>Submit!</button>
        </form>
        {/* Change code below this line */}

        {/* Change code above this line */}
      </div>
    );
  }
}

Your browser information:

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

Challenge: React - Create a Controlled Form

Link to the challenge:

You have not completed the following:

In the render method, create the input element above the h4 tag. Add a value attribute which is equal to the input property of the component’s state . You did the first part but not the second part.

Thanks, I was confused of why I m not passing.

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