一个组件中的多个表单无法识别表单和初始值属性(redux-form v7.0.4)

2024-06-01

我正在单个组件中创建多个表单,并使用我定义表单的 redux 存储来初始化它们name属性中的<form>元素,而不是在reduxForm()助手,记录在这里......

如何在页面上多次嵌入相同的 redux-form ? https://stackoverflow.com/questions/37456526/how-to-embed-the-same-redux-form-multiple-times-on-a-page/37464048#37464048

我正在从“列表”对象创建表单并将其传递给我的组件mapStateToProps()。我正在尝试设置表单的初始值initialValues={},但是 Redux Form 会产生以下错误,并要求在reduxForm()帮手...

1)失败的道具类型:道具form已按要求标记为Form(ItemInfo),但其值为undefined.

2)未知道具initialValues在标签上。从元素中删除此道具。

这似乎与这里提到的问题相似......

https://github.com/erikras/redux-form/issues/28 https://github.com/erikras/redux-form/issues/28

import _ from 'lodash';
import React, { Component } from 'react';
import { reduxForm, Field } from 'redux-form';
import { connect } from 'react-redux';
import * as actions from '../../../actions';
import {Col} from 'react-grid-system';
import RaisedButton from 'material-ui/RaisedButton';

class ItemInfo extends Component {

  renderSingleItem(item){
    let theItem =  _.map(_.omit(item, '_id'), (value,field) => {
        return (
          <div key={field}>
            <label>{field}</label>
            <Field component="input" type="text" name={field} style={{ marginBottom: '5px' }} />
            <div className="red-text" style={{ marginBottom: '20px' }}>
            </div>
          </div>
        );
      });
    return theItem || <div></div>;
  }

  renderItemInfo() {

      if (this.props.listing.listing !== undefined) {
        let theItems = _.map(this.props.listing.listing.items, item => {                
            return (
                <Col key={item._id} md={3}>
                  <form form={`editItemInfo_${item._id}`} initialValues={item}>
                    {this.renderSingleItem(item)}
                    <RaisedButton secondary={true} label="Remove Item"/>
                    <RaisedButton primary={true} label="Update Item"/>
                  </form>
                </Col>
            );
        });
        return theItems || <div></div>;
      }

  }

  render() {
    return (
        <div className="row">
            {this.renderItemInfo()}
        </div>
    );
  }
}

function mapStateToProps({listing}) {
  return { listing };
}

ItemInfo = reduxForm({
  fields: ["text"],
  enableReinitialize: true
})(ItemInfo)

ItemInfo = connect(mapStateToProps,actions)(ItemInfo)

export default ItemInfo

这是返回“列表”对象的示例......

{ _id: 59b5eebd33a3a833b6ac1386,
  _user: 59aff09a11011f0cfd8d7666,
  location: 'nother',
  availability: 'jhkvljh',
  price: 9860,
  __v: 0,
  items:
   [ { equipmentType: 'kbj;kj',
       make: ';jb',
       model: ';;jb',
       watts: 9860,
       bulb: 'kjbkj',
       condition: 'kjbkjb',
       _id: 59b5eebd33a3a833b6ac1387 },
     { condition: 'houy',
       bulb: 'jhg',
       watts: 8907,
       model: 'mode',
       make: 'maker',
       equipmentType: 'smoquip',
       _id: 59b5f9cf13b37234ed033a75 } ] }

感谢您的帮助!


我终于通过一点小技巧找到了解决方法。看起来这一部分是 Redux Form 的错误,一部分是我最初实现的错误。


正确实施

As detailed by @erikras, Redux Form creator... enter image description here https://github.com/erikras/redux-form/issues/28 https://github.com/erikras/redux-form/issues/28

表单配置参数需要传递给装饰组件而不是 jsx<form>成分。为此,我将表单重构为导入的子组件并映射到这些组件上......

 renderItemForms() {
    if (this.props.listing.listing !== undefined) {
      return _.map(this.props.listing.listing.items, item => {
          return (
            <ItemInfo form={`editItemInfo_${item._id}`} initialValues={item} key={item._id} item={item} /> 
          );
      });
    }
  }



Redux 表单错误

上面的实现会将你的表单正确连接到 redux 存储,但它仍然会创建一个“道具类型失败:道具形式已标记为必需”错误会破坏你的观点。我找到的解决方案是将任何随机字符串粘贴在“form”属性中redux-form防止错误的选项...

ItemInfo = reduxForm({
  form: 'any random string here',
  fields: ["text"],
  enableReinitialize: true
})(ItemInfo)

第二条错误消息为initialValues仅在第一个“表单参数”错误之后,所以现在一切都没有错误,并且在 Redux 开发工具中我可以确认内联表单属性正在覆盖来自的属性reduxForm()选项。现在,redux 存储已使用正确的“表单名称/id”成功管理表单...



我希望这可以帮助其他人避免轻微的头痛。请原谅我上面的解释中的任何不正确的术语,我仍然是 Redux/React 菜鸟,但如果有人想要更多详细信息,我很乐意提供有关我的实现的更多详细信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一个组件中的多个表单无法识别表单和初始值属性(redux-form v7.0.4) 的相关文章

随机推荐