Create a Controlled Form React

Tell us what’s happening:

This event.preventDefault() is really messing me up. I know I have to add something to the handleSubmit besides that but keep getting an error. Also my

message was showing to be working right and now it is not … Thanks for any help!

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
      this.setState({
        submit: event.preventDefault()
      })
    };
    // change code above this line
  
  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          { /* change code below this line */ }
          <input value={this.state.input} onChange={this.handleChange} />
          { /* change code above this line */ }
          <button type='submit'>Submit!</button>
        </form>
        { /* change code below this line */ }
        <h1>{this.state.submit}</h1>
        { /* change code above this line */ }
      </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/66.0.3359.181 Safari/537.36.

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

event.preventDefault() is a method often used in plain js too ^^ It’s used to prevent the default behaviour of something ( as the name suggests ^^)

this.setState is strictly related to the react component - it’s used to change the state of the component, and in this specific case

should […] sets the component state property submit to the current input value

The event.preventDefault should be used before to change the state in your handleSubmit^^

Good luck!

You misunderstood the text, you have to write like this.

handleSubmit(event) {
    // change code below this line
    event.preventDefault() ; // this stops the event
      this.setState({
        submit:  // Add code for submit, so it updates the <h1>{this.state.submit}</h1>
      })
    // change code above this line
};

I suggest you reset your code first because you have messed up the existing code.

Got it! Thanks for your help. Forms always give me trouble

No response when I click Run test. Can anyone tell me what is wrong with the code.
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 (



{ /* change code below this line / }
<input type=“text” value={this.state.input} onChange={this.handleChange}
{ /
change code above this line / }
Submit!

{ /
change code below this line */ }

{this.state.submit}

{ /* change code above this line */ }
); } };

where’s your <h1>{this.state.submit}</h1> tag?