Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Form Component onSubmit Handler Not Working

Tags:

reactjs

I have the following React component:

class Form extends React.Component {
  handleSubmit(e) {
    e.preventDefault();

    let loginInput = ReactDOM.findDOMNode(this.refs.login);

    // change Main component's state
    this.props.addCard(loginInput.value);

    // reset the form
    loginInput.value = '';
  }

  render() {
    return(
      <form onSubmit={this.handleSubmit}>
        <input placeholder="githug login" ref="login" />
        <button>Add Login</button>
      </form>
    );
  }
}

As you can see, I want the handleSubmit function to be called whenever the form is submitted. I have indicated this by adding the function to the onSubmit handler.

The function is being invoked at the correct time. However, the value of this within that function is null. This is surprising to me, as I expected the value of this to be the React Component. The fact that this is null is surprising to me, because I am using a very similar logic / code as suggest by the official React documentation.

I would appreciate the help in figuring out why this is not the React component, as expected, and how to fix the code so that it is.

like image 808
Steven L. Avatar asked Jan 30 '16 05:01

Steven L.


People also ask

Why onSubmit is not being called?

The onsubmit handler is not called, because the form cannot be submitted by any normal means, i.e. the submit event cannot be caused. There is only one submit control, and it is declared as disabled. Save this answer.

Why submit button is not working in react JS?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

Why we use handleSubmit in react JS?

You can easily submit form asynchronously with handleSubmit. disabled inputs will appear as undefined values in form values. If you want to prevent users from updating an input and wish to retain the form value, you can use readOnly or disable the entire <fieldset />. Here is an example.


3 Answers

When you use React with ES2015 classes you should set this to event handlers

class Form extends React.Component {
  constructor(props) {
     super(props);
     this.handleSubmit = this.handleSubmit.bind(this);
  }    

  handleSubmit(e) {
    e.preventDefault();

    let loginInput = this.refs.login;
    this.props.addCard(loginInput.value);
    loginInput.value = '';
  }

  render() {
    return(
      <form onSubmit={ this.handleSubmit }>
        <input placeholder="githug login" ref="login" />
        <button>Add Login</button>
      </form>
    );
  }
}

Example

No Autobinding

Methods follow the same semantics as regular ES6 classes, meaning that they don't automatically bind this to the instance. You'll have to explicitly use .bind(this) or arrow functions =>.

like image 55
Oleksandr T. Avatar answered Oct 16 '22 09:10

Oleksandr T.


You need to use button or input with type="submit"

<button type="submit">Submit</button>

Or

<input type="submit" value="Submit" />
like image 19
ma_dev_15 Avatar answered Oct 16 '22 09:10

ma_dev_15


One obvious piece of information: do not forget to include your button inside the <form>. I got stuck for a while until I figured out I had put my submit button outside my form, like this:

  <form onSubmit={ this.handleSubmit }>
    <input placeholder="githug login" ref="login" />
  </form>            
  <button type="submit">Add Login</button>

hence the onSubmit event was not being called, and would never be.

like image 11
K09P Avatar answered Oct 16 '22 09:10

K09P