zoukankan      html  css  js  c++  java
  • reducer-form 数组字段 在removeField/removeField 之后 dirty 不改变的问题

    reducer-form 数组字段 在removeField/removeField 之后 dirty 不改变

    最近在用reducer-form 做项目时 如果字段类型是数组 , 在进行removeField/removeField 之后 this.props.dirty 死活不改变 于是google了一下 终于在github的issues上面找到了答案
    原来这是reducer-form的一个bug 解决方案暂时只能用hack的方法做了

    解决方法如下

    添加一个字段touched来保存数组字段的dirty 并且在removeField/removeField 之后 调用 touched.onChange(true);

    const LinkedProductsForm = reduxForm({
      form: formName,
      fields: [
        'id',
        'touched', // hack
        'product_ids[]'
      ]
    }, undefined, {
      addProduct: () => addArrayValue(formName, 'product_ids')
    })(Form);
      unlinkProduct(index) {
        const {fields: {touched, product_ids: productIds}} = this.props;
        return () => {
          productIds.removeField(index);
          touched.onChange(true);
        }
      }
      
    <LinkedProduct
      key={`linked-product-${index}`}
      productId={productId}
      product={this.getProduct(productId)}
      unlink={this.unlinkProduct(index)}
      loadProducts={loadProducts} />
    
  • 相关阅读:
    设计模式七大原则之单一职责原则
    机器学习入门与进阶
    Django之路
    Python编程之路
    Python练习1
    Docker入门与进阶
    运维相关
    Node.js(一)
    位运算
    双指针算法
  • 原文地址:https://www.cnblogs.com/ElvinLong/p/5446716.html
Copyright © 2011-2022 走看看