Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux- Form: Unable to Dispatch Action on Submit

I'm using redux-form in my React / Redux application, and I'm trying to figure out how to dispatch an action on submit.

I have been able to trigger the handleSubmit function, and the submit function that I pass to it is executed, but the 'submitFormValues' action is not called.

I've tried using mapDispatchToProps and connect(), but that doesn't work either.

The Redux-Form actions execute (START_SUBMIT, STOP_SUBMIT, SET_SUBMIT_SUCCEEDED), but my own action creator is never executed.

Here's the form:

import React from "react";
import { Field, reduxForm } from "redux-form";
import {submitFormValues} from "../../../actions/BasicFormActions";


function submit(values) {
    //Can log the values to the console, but submitFormValues actionCreator does not appear to be dispatched.
    return new Promise(function(resolve) { resolve(submitFormValues(values))} )
}

const renderField = ({ input, label, type, meta: {touched, error} }) => (
    <div>
        <label>{label}</label>
        <div>
            <input {...input} placeholder={label} type={type}/>
            {touched && error && <span>{error}</span>}
        </div>
    </div>
)

const BasicForm = (props) => {
    const { error, handleSubmit, pristine, reset, submitting } = props;
    return (
        <form onSubmit={handleSubmit(submit)}>
            <Field name="firstName" type="text" component={renderField} label="First Name"/>
            <Field name="lastName" type="text" component={renderField} label="Last Name"/>
            {error && <strong>{error}</strong>}
            <div>
                <button type="submit" disabled={submitting}>Submit</button>
                <button type="button" disabled={pristine || submitting} onClick={reset}>Clear</button>
            </div>
        </form>
    )
}



export default reduxForm({
    form: "basicForm",
    submit
})(BasicForm)

Here's the action Creator (uses Thunk). I'm able to dispatch these actions successfully, just not from the form.

 export const submitFormValues = (values) => (dispatch) =>
        getData("submitApproveForm", values).then(response => {
            dispatch(formSubmissionError(response))
        }).catch(error => {
            throw (error);
        });

const formSubmissionError = (response) =>
    ({
        type: types.FORM_SUBMISSION_ERROR,
        basicFormResponse: { ...response}
    });


export const getData = (apiName, args) =>
    fetch(settings.basePath + getUrl(apiName, args))
        .then(response =>
    response.json()
    ).catch(error => {
        return error;
    });

Finally my reducer:

import * as types from "../actions/ActionsTypes";
import initialState from "./initialState";

const basicFormReducer = (state = initialState.basicFormResponse, action) => {
    switch (action.type) {
        case types.FORM_SUBMISSION_ERROR:
            return {...state, "errors": action.basicFormResponse}; // returns a new state
        case types.FORM_SUBMISSION_SUCCESS:
            return {...state, "successes": action.basicFormResponse}; // returns a new state
        default:
            return state;
    }
};

export default basicFormReducer;

Thank you in advance for your help.

like image 630
Julie Torres Avatar asked Mar 21 '17 02:03

Julie Torres


1 Answers

Redux Form does not dispatch anything in the onSubmit callback. This is because we make no assumptions regarding how you would like to deal with your form submissions.

You can, however, use the dispatch argument and... dispatch your action!

function submit(values, dispatch) {
    return dispatch(submitFormValues(values));
}
like image 80
gustavohenke Avatar answered Nov 16 '22 03:11

gustavohenke