question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remote submit breaks when submit function passed to handleSubmit

See original GitHub issue

I’ve been messing around with remote submit lately and it works beautifully whenever I pass my submit function to reduxForm() as a config value. However, if I instead supply it to my handleSubmit() function on the form, my app breaks with the following error:

You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop

Note that I am using redux-form/immutable and that I’m using stuff like bindActionCreators so maybe there is some weird combination there or maybe my implementation is simply wrong, but this seems like a bug to me.

I’m using: redux-form v6.2.1 redux v3.6.0 react v15.3.1

Here’s a simplified version of my code:

Wrapper.js

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { submit } from 'redux-form/immutable';

const mapDispatchToProps = function (dispatch) {
  return bindActionCreators({
    submit: submit
  }, dispatch);
};

class Wrapper extends Component {
  render() {
    return (
      <MyForm />
      <SubmitButton submit={submit} />
    );
}

export default connect(mapStateToProps, mapDispatchToProps)(Wrapper);

MyForm.js

import validate from './validate';

class MyForm extends Component {
  mySubmitFunction = (values) => {
    console.log('submitted!');
  }

  render() {
    const { handleSubmit } = this.props;

    return (
      <form onSubmit ={handleSubmit(this.mySubmitFunction)}>
        <Field ...fieldProps />
      </form>
    );
  }
}

export default reduxForm({ form: 'myForm', validate })(MyForm);

SubmitButton.js

export default class SubmitButton extends Component {
  submit = () => {
    this.props.submit('myForm');
  }

  render() {
    return <button onClick={this.submit}>Submit</button>
  }
}

Shouldn’t this code work? I’m making my submit function local to the MyForm class here, but it still breaks if it is an external function. Once again, this works perfectly if I pass an external submit function to reduxForm() instead. Is this a bug?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
davidkpianocommented, Dec 21, 2016

4th option: a <Form> component that’s just a shallow wrapper for <form>, which will already know what is passed to its onSubmit so you can use your redux-dead-drop strategy to listen for dispatched submit actions and call submit?

E.g. <Form onSubmit={handleSubmit(...)}>

10reactions
mdrideoutcommented, Jul 6, 2018

I struggled with this for a few hours. And it was as simple as including “Form” in the redux-form import statement

import { Form, Field, reduxForm, SubmissionError } from 'redux-form';

And changing to an uppercase “Form” for my form tag.

<Form name={formName} id={formName} onSubmit={handleSubmit(() => this.submitForm())}>

And I can now remotely submit the form from an action

// Submit the form
dispatch(submit(formName));

I agree that the documentation should be updated to include an example like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux-Form can't do a remote submit - Stack Overflow
Declare handleSubmit function that dispatch(submit('form name')); Pass handleSubmit & handleModalClose (for use at onSubmit spot) via props ...
Read more >
useForm - handleSubmit - React Hook Form
This function will receive the form data if form validation is successful. Props. Name, Type, Description. SubmitHandler, (data: Object, e?: Event) => void ......
Read more >
The Complete Guide to Building React Forms with useState ...
This tutorial teaches how to build forms in react with uesState hook, how to pass the data as props to another react component...
Read more >
onsubmit Event - W3Schools
Execute a JavaScript when a form is submitted: <form onsubmit="myFunction()"> Enter name: <input type="text"> <input type="submit"> </form>.
Read more >
Remote Submit Example - Redux Form
Notice that for this to work, the submit function must be given to the form component via either a config value passed to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found