【问题标题】:Redux form with self creating inputs具有自创输入的 Redux 表单
【发布时间】:2023-03-11 02:02:02
【问题描述】:

我正在尝试创建一个可以动态创建自己的输入的 redux 表单(使用 redux-form)。我无法弄清楚如何让 redux-form 了解已创建的新字段。是否可以在表单组件本身中动态更改 redux-form 传入的 fields 属性?我在想这个错误吗?这是我正在使用的。

class AddCustomer extends Component {

 render() {

  class Form extends Component {

    constructor(props, context) {
      super(props, context)
      this.state = {
        inputsToAdd: []
      };
    }

    handleAddInput() {
       let inputsToAdd = this.state.inputsToAdd.slice();
       inputsToAdd.push(this.state.inputsToAdd.length);
       this.setState({ inputsToAdd });
    }

    submitForm(data) {
       console.log(data)
       this.setState({inputsToAdd: []});
       this.props.dispatch(initialize('addCustomer', {}))
    }

    render() {
      const { fields, handleSubmit } = this.props;
      return (
          <div>
            <form onSubmit={handleSubmit(this.submitForm.bind(this))}>
              <Input type='text' label='Company name' {...fields['companyName']}/>
              <Input type='email' label='Admin user email' {...fields['adminEmail']}/>
            </form>
            {this.state.inputsToAdd.map((element, index) => {
              return (
                <Input key={index} type='text' />
              )
            })}
            <Button onClick={() => this.handleAddInput()}>Add Input</Button>
            <Button onClick={handleSubmit(this.submitForm.bind(this))}>Submit</Button>
          </div>
        )
      }
    }

    Form = connectReduxForm({
      form: 'addCustomer',
      fields: ['companyName', 'adminEmail']
    })(Form);

    return (
      <div>
        <h1>Add Customer</h1>
        <Form />
      </div>
    )
  }
}

【问题讨论】:

    标签: reactjs redux redux-form


    【解决方案1】:

    从 Redux Form 6.* 开始,您可以使用 &lt;FieldArray/&gt; 实现您想要做的事情

    参见下面的示例(取自 Redux 文档并略微简化)

    import React from 'react'
    import { Field, FieldArray, reduxForm } from 'redux-form'
    import validate from './validate'
    
    const renderMembers = ({ fields, meta: { touched, error } }) => (
      <ul>
        <li>
          <button type="button" onClick={() => fields.push({})}>Add Member</button>
          {touched && error && <span>{error}</span>}
        </li>
        {fields.map((member, index) =>
          <li key={index}>
            <button
              type="button"
              title="Remove Member"
              onClick={() => fields.remove(index)}/>
            <h4>Member #{index + 1}</h4>
            <Field
              name={`${member}.firstName`}
              type="text"
              component={renderField}
              label="First Name"/>
            <Field
              name={`${member}.lastName`}
              type="text"
              component={renderField}
              label="Last Name"/>
    
          </li>
        )}
      </ul>
    )
    
    
    const FieldArraysForm = (props) => {
    const { handleSubmit, submitting } = props
      return (
        <form onSubmit={handleSubmit}>
          <FieldArray name="members" component={renderMembers}/>
          <div>
            <button type="submit" disabled={submitting}>Submit</button>
          </div>
        </form>
      )
    }
    
    export default reduxForm({
      form: 'fieldArrays',     // a unique identifier for this form
      validate
    })(FieldArraysForm)
    

    有关更多信息,请查看文档 http://redux-form.com/6.1.1/examples/fieldArrays/

    【讨论】:

    • 那么 redux form 5 怎么样...我知道它很旧,但我在工作中使用旧的代码库,并且在这种规模的项目中升级并不是一个真正的选择...
    猜你喜欢
    • 2019-10-21
    • 2023-03-21
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2016-01-20
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多