Redux- 表单:无法在提交时发送操作
Redux- Form: Unable to Dispatch Action on Submit
我在我的 React / Redux 应用程序中使用 redux-form,我正在尝试弄清楚如何在提交时分派操作。
我已经能够触发 handleSubmit 函数,并且执行了我传递给它的提交函数,但是没有调用 'submitFormValues' 操作。
我尝试过使用 mapDispatchToProps 和 connect(),但这也不起作用。
Redux-Form 动作执行(START_SUBMIT、STOP_SUBMIT、SET_SUBMIT_SUCCEEDED),但我自己的动作创建器从未执行过。
表格如下:
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)
这是动作 Creator(使用 Thunk)。我能够成功地发送这些操作,而不是从表单中发送。
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;
});
最后是我的减速器:
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;
预先感谢您的帮助。
Redux Form 不会在 onSubmit
回调中调度任何内容。这是因为我们不会假设您希望如何处理您的表单提交。
但是,您可以 use the dispatch
argument 并...发送您的操作!
function submit(values, dispatch) {
return dispatch(submitFormValues(values));
}
我在我的 React / Redux 应用程序中使用 redux-form,我正在尝试弄清楚如何在提交时分派操作。
我已经能够触发 handleSubmit 函数,并且执行了我传递给它的提交函数,但是没有调用 'submitFormValues' 操作。
我尝试过使用 mapDispatchToProps 和 connect(),但这也不起作用。
Redux-Form 动作执行(START_SUBMIT、STOP_SUBMIT、SET_SUBMIT_SUCCEEDED),但我自己的动作创建器从未执行过。
表格如下:
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)
这是动作 Creator(使用 Thunk)。我能够成功地发送这些操作,而不是从表单中发送。
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;
});
最后是我的减速器:
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;
预先感谢您的帮助。
Redux Form 不会在 onSubmit
回调中调度任何内容。这是因为我们不会假设您希望如何处理您的表单提交。
但是,您可以 use the dispatch
argument 并...发送您的操作!
function submit(values, dispatch) {
return dispatch(submitFormValues(values));
}