Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handleSubmit with a redux-form

I'm working to use redux-form for the first time. I am able to render the form but I have not been able to handle the submit. While I eventually want to send the data to the server, at this point, I'm simply trying to console log the form field values. I'm getting the error:

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

Here's my Profile.jsx file

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {withAuth} from 'react-devise';
import { Field, reduxForm } from 'redux-form';

class Profile extends Component {
  handleSubmit(data) {
     console.log('Submission received!', data);
   }
  render() {
    const { handleSubmit } = this.props;
    return (
      <form onSubmit={handleSubmit}>
        <div>
          <label htmlFor="firstName">First Name</label>
          <Field name="firstName" component="input" type="text"/>
        </div>
        <div>
          <label htmlFor="lastName">Last Name</label>
          <Field name="lastName" component="input" type="text"/>
        </div>
        <div>
          <label htmlFor="email">Email</label>
          <Field name="email" component="input" type="email"/>
        </div>
        <button type="submit">Submit</button>
      </form>
    );
  }
}

// Decorate the form component
Profile = reduxForm({
  form: 'profile' // a unique name for this form
})(Profile);


const mapStateToProps = state => {
  return {
    currentUser: state.currentUser
  };
};

export default connect(mapStateToProps)(withAuth(Profile));

How can I handle the submitted values in a way where I can eventually send them to my API?

like image 334
AnApprentice Avatar asked May 14 '17 17:05

AnApprentice


People also ask

What is pristine in Redux form?

pristine means that no fields in the form have been modified yet. Perhaps you won't be able to find an exact definition of it in docs, but there is a similar terminology in Angular. You can find some details here or here. submitting , as the name suggests, means that the form is in process of submitting.

What is handleSubmit?

handleSubmit gets the current value of state. value and adds it to the array of webhooks . The rest of App is responsible for rendering the other React components that make up the page and passing data to the components that require it. I hope this helps you to understand how to create submit button forms in React.

Should I use Redux form?

Redux-form is a really great library for working with validations. You can simply develop a lot of validations for different situations. Hence it provides validation functions to validate all the values in your form at once. You may also provide individual value validation functions for each Field or FieldArray.

What are the main features of Redux form?

Some of the main features of Redux Form are: Field values persistence via Redux store. Validation (sync/async) and submission. Formatting, parsing and normalization of field values.


1 Answers

Redux-Form decorates your component with handleSubmit prop. According to docs it's:

a function meant to be passed to <form onSubmit={handleSubmit}> or to <button onClick={handleSubmit}>. It will run validation, both sync and async, and, if the form is valid, it will call this.props.onSubmit(data) with the contents of the form data.

Optionally, you may also pass your onSubmit function to handleSubmit which will take the place of the onSubmit prop. For example:

So if your component doesn't have onSubmit property you have to 'manually' pass your submit handler to handleSubmit function. Please try this:

<form onSubmit={this.props.handleSubmit(this.handleSubmit.bind(this))}>

Please don't confuse your handleSubmit method with prop passed from Redux-Form with the same name.

like image 117
Bartek Fryzowicz Avatar answered Oct 28 '22 20:10

Bartek Fryzowicz