1

我正在使用来自: http ://redux-form.com/4.2.0 的 redux-form ,我尝试了简单的联系表单示例,您可以在此处看到:

import React, {Component} from 'react';
import {reduxForm} from 'redux-form';
import TitleBlock from './TitleBlock'

class ContactForm extends Component {
    render() {
        const {fields: {firstName, lastName, email}, handleSubmit} = this.props;
        return (
            <form onSubmit={handleSubmit}>
                <div>
                    <label>First Name</label>
                    <input type="text" placeholder="First Name" {...firstName}/>
                </div>
                <div>
                    <label>Last Name</label>
                    <input type="text" placeholder="Last Name" {...lastName}/>
                </div>
                <div>
                    <label>Email</label>
                    <input type="email" placeholder="Email" {...email}/>
                </div>
                <TitleBlock />
                <button type="submit">Submit</button>
            </form>
        );
    }
}

ContactForm = reduxForm({ // <----- THIS IS THE IMPORTANT PART!
    form: 'contact',                           // a unique name for this form
    fields: ['firstName', 'lastName', 'email'] // all the fields in your form
})(ContactForm);

export default ContactForm;

它工作得很好,但我想分离我的组件来创建带有自定义块的表单,例如我在这里创建了一个标题块:

import React, { Component, PropTypes } from 'react'
import {reduxForm} from 'redux-form';
export const fields = ['title', 'description'];

export default class TitleBlock extends Component {

    static propTypes = {
        fields: PropTypes.object.isRequired,
    };

    render() {
        const {
            fields: {title,description},
            } = this.props;

        return (
            <div>
                <div>
                    <label>Title</label>
                    <div>
                        <input type="text" placeholder="Title" {...title}/>
                    </div>
                </div>
                <div>
                    <label>Description</label>
                    <div>
                        <input type="text" placeholder="Description" {...description}/>
                    </div>
                </div>
            </div>

        )
    }
}
export default reduxForm({
    form: 'TitleBlock',
    fields
})(TitleBlock);

我想将此 TitleBlock 与我的联系表格互锁,是否可以这样做并在一个提交功能中管理所有信息?

4

1 回答 1

1

您可以让组件将prop传递给,而不是连接TitleBlock到,如下所示:redux-formContactFormfieldsTitleBlock

class ContactForm extends React.Component {
    ...
    render() {
        return (
            <div>
                <TitleBlock fields={this.props.fields} />
                ...
            </div>
        );
    }
}

export default reduxForm({...})(ContactForm)

您的TitleBlock组件可能如下所示:

export default class TitleBlock extends Component {
    static propTypes = {
        fields: PropTypes.object.isRequired,
    };

    render() {
        const { fields: { title, description } } = this.props;
        return (
            <div>
                <div>
                    <label>Title</label>
                    <div>
                        <input type="text" placeholder="Title" {...title}/>
                    </div>
                </div>
                <div>
                    <label>Description</label>
                    <div>
                        <input type="text" placeholder="Description" {...description}/>
                    </div>
                </div>
            </div>
        );
    }
}
于 2016-03-11T22:49:36.270 回答