繁体   English   中英

如何使用 redux 存储变量更新 FieldArray 元素

[英]how to update FieldArray elements with redux store variable

  • 我正在使用带有 FieldArray 的 redux-form。默认情况下,数组中有 1 个元素,它是从 JSON 填充的。 我最多可以在 FieldArray 组件中添加 3 个元素。

  • 在下面的代码中,“elementList”属性来自 JSON,并且我还存储了名为“elements”和“elementList”的变量。 我首先使用 JSON 中的 elementList 初始化这些存储变量,然后在单击“添加元素”时继续更新存储变量。 我可以看到存储变量正在正确更新,但屏幕上的字段数组元素没有更新。这可能是因为 FieldArray 中的名称属性“elementList”可能引用了 JSON 元素。

    是否有可能,如果我可以在“FieldArray”的名称属性中引用存储变量“elementList”或“elements”。 请指教。

主页

  <div>
      <FieldArray name="elementList" component={Elements}
                  props={this.props}/>
      <button type="button" className="btn btn-primary"
              onClick={event => this.addElement(elementDTO)}>Add Element
      </button>
      <br/>
  </div>
  
  addElement(elementDTO){
        if(this.props.elements && this.props.elements!=undefined && this.props.elements.length >= 3){
            return;
        }
        this.props.addReqElement(this.props.elements);
    }

字段数组页面

const elements= ({props, meta: {error, submitFailed}}) => {
    const {fields} = props;
    return (
    {fields.map((element, index) => (
     <div> 
            //Field definitions
     </div>
))}

谢谢

更新:从 Redux Action 和 Reducer 添加方法

 export function addReqElement(childList) {
         let state = store.getState()
         let newChild= 
            state.requestReducer.DTOobj.requestDoc;  //this is an empty element coming from backend with few properties and adding rest of the //proerties as below to create a new child 
        newChild.prop1 = null
        newChild.prop2 = null
        childList.push(newChild)
        return (dispatch) => {
            dispatch(setDoc(childList));
        }
    }
    

export function setDoc(payload) {
    return {
        type: ADD_DOC,
        payload: payload
    }
}

更新 2:我尝试删除 push 并使用传播运算符,但它不起作用。 我也有内部数组,因为我正在使用不同的策略。 我取拉父数组,它是索引并用新的内部数组更新父数组。 它可以工作,但是父数组我不知道我应该如何使它工作。 我试图将主数组设置为表单道具并通过调度一个动作来呈现整个页面,但它不起作用。 有什么建议吗?

从主阵列页面:

render() {
   const {fields, parentArrayFromStore} = this.props;
    return (
       <div className="col-sm-12">
          {fields.map((doc, index) => (
           <div key={index}>
            <div className="col-sm-12">
              <FieldArray name={`${doc}.innerArrayList`} component={CustomInnerArrayComponent}/>
            </div>
            <div className="row">
               <div className="col-sm-4">
                  <button type="button" className="btn btn-primary"
                          onClick={event => this.addInnerArray(index, parentArrayFromStore ,fields.get(index).innerArrayList)}>Add Printer
                   </button>
                </div>
            </div>
        </div>
    ))}
    </div>)
            }
                
        

在行动 class

export function addInnerArray(index, parentArrayFromStore, innerArrayList) {
    let newInnerItem= {};
    newInnerItem.prop1 = null
    newInnerItem.prop2 = null
   
    innerArrayList.push(newInnerItem)
    parentArrayFromStore[index].innerArrayList = innerArrayList;

    return (dispatch) => {
        dispatch(setParentArray(parentArrayFromStore));
    }
}

export function setParentArray(payload) {
    return {
        type: ADD_DOC,
        payload: payload
    }
}

嗨,问题在于您的 function 中的 push 语句在更新构造函数或减速器中的状态时使用 concat 或 spread operator[...]> 我在这里做了一个示例,请检查

onAddItem() {
        let list = [...this.state.items, {text:`Check 1`}];
        this.setState({items:list});
}

在您的情况下,您可以执行以下操作

let arr = [...childList, newChild]

然后发送 arr

 dispatch(setDoc(arr));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM