有没有办法在 react-final-form 中使用 Redux actions 和 reducer?

Is there a way to use Redux actions and reducer with react-final-form?

我有一个通过常规 React class 组件创建的表单,我正在尝试迁移以使用 'react-final-form'。一切看起来都不错,但无法找到使用 'react-final-form'

的减速器的方法

最初,我使用来自 react 的 bindActionCreators 将 Dispatch 映射到 Props。因此,它过去常常调用操作文件,一切都很好。不确定是否可以使用相同的功能。我试图直接从 onSubmit 调用操作,但那里未定义调度。我什至找不到任何涵盖这部分的教程。

注册表单:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import {Link} from 'react-router-dom'
import {Form, Field} from 'react-final-form';


// custom modules
import { submitEmail } from '../../actions/submitEmail';

import '../../styles/custom.css';


//Form through react-final-form
const renderInput = ({input, meta}) => (
    <input {...input} type="text" errorMessage={meta.touched && meta.error} />
);

const onSubmit = values => {
    debugger;
    submitEmail('/freeTrial', values);
    console.log("put request sent for trial ")
    alert(JSON.stringify(values))
};

const required = v => {
    console.log("v....: ", v);
    if (!v || v === ''){
        return 'This field is required';
    }

    return undefined;
};


const SendEmailInfo = () =>  (
    <Form
        onSubmit={onSubmit}
        render={({handleSubmit, invalid}) => (
            <div>
                <h2>Request 30 days Free Trial</h2>
                <form  onSubmit={handleSubmit} >
                    <Field name="customer-id"
                           component={renderInput}
                           validate={required} />
                    <button type="submit" disabled={invalid} > Submit </button>
                </form>
            </div>
        )}
    />
);

const mapDispatchToProps = dispatch => {
    return bindActionCreators(
        { submitEmail }, dispatch);
};


export default connect(null, mapDispatchToProps)(SendEmailInfo)

提交电子邮件

import { API_URL } from '../global';

export const makeAPIRequestToSendEmail = () => {
    return {
        type: 'MAKE_API_REQUEST_TO_SEND_EMAIL'
    };
};

export const clearEmailDetails = () => {
    return {
        type: 'CLEAR_EMAIL_DETAILS'
    };
};

export const receivedResponse = () => {
    return {
        type: 'RECEIVED_RESPONSE'
    };
};

export const sentEmail = data => {
    return {
        type: 'SENT_EMAIL',
        data
    };
};

export const errorEmail = () => {
    return {
        type: 'ERROR_EMAIL'
    };
};

export function submitEmail(url, data) {
    return dispatch => {
        dispatch(makeAPIRequestToSendEmail());
        dispatch(clearEmailDetails());
        return fetch(API_URL + url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ data })
        })
            .then(response => response.json())
            .then(emailMessage => {
                dispatch(receivedResponse());
                console.log(emailMessage);
                dispatch(sentEmail(emailMessage));
            })
            .catch(err => dispatch(errorEmail()));
    }
}

我想使用 actions 和 reducers,让它处理所有 API 请求。如果您有任何解决此难题的建议,请告诉我。

所有 React Final Form 关心的是你给它一个 onSubmit 那个 returns 一个 Promise。我已经编写了一个库来使用可能满足您特定需求的 Redux 操作来执行此操作?

React Redux Promise Listener