简体   繁体   English

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

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

  • I am using redux-form with a FieldArray.By default 1 element will be there in array and it is populated from JSON.我正在使用带有 FieldArray 的 redux-form。默认情况下,数组中有 1 个元素,它是从 JSON 填充的。 I can add upto 3 elements in FieldArray component.我最多可以在 FieldArray 组件中添加 3 个元素。

  • In below code, 'elementList' property is coming from JSON and also I have store variables named as'elements' and 'elementList'.在下面的代码中,“elementList”属性来自 JSON,并且我还存储了名为“elements”和“elementList”的变量。 I initialize these store variable with elementList from JSON at first and then keep updating store variables when 'Add Element' is clicked on.我首先使用 JSON 中的 elementList 初始化这些存储变量,然后在单击“添加元素”时继续更新存储变量。 I can see store variables are updating properly but on screen Field array elements are not updating.It may be because name property 'elementList' in FieldArray may refer to JSON element.我可以看到存储变量正在正确更新,但屏幕上的字段数组元素没有更新。这可能是因为 FieldArray 中的名称属性“elementList”可能引用了 JSON 元素。

    Is it possible, if I can refer to store variables 'elementList' or 'elements' in name property of 'FieldArray'.是否有可能,如果我可以在“FieldArray”的名称属性中引用存储变量“elementList”或“elements”。 Please advice.请指教。

Main page主页

  <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);
    }

Field Array page字段数组页面

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

Thank you谢谢

Update: Adding method from Redux Action and Reducer更新:从 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
    }
}

Update 2: I tried to remove push and used spread operator, but it did not work.更新 2:我尝试删除 push 并使用传播运算符,但它不起作用。 I have inner array also, that is working as I am using different strategy.我也有内部数组,因为我正在使用不同的策略。 I take pull parent array,it's index and update parent array with the new inner array.我取拉父数组,它是索引并用新的内部数组更新父数组。 It works but parent array I am not getting how should I make it work.它可以工作,但是父数组我不知道我应该如何使它工作。 I tried to set the main array to the form props and render full page by dispatching an action but it did not work.我试图将主数组设置为表单道具并通过调度一个动作来呈现整个页面,但它不起作用。 Any suggestions plz?有什么建议吗?

From the main array page:从主阵列页面:

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>)
            }
                
        

In Action class在行动 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
    }
}

Hi the issue is with the push statement in your function when updating states in the constructor or reducer use concat or spread operator[...]> I have made a sample over here please check嗨,问题在于您的 function 中的 push 语句在更新构造函数或减速器中的状态时使用 concat 或 spread operator[...]> 我在这里做了一个示例,请检查

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

in your case you can do the following在您的情况下,您可以执行以下操作

let arr = [...childList, newChild]

then dispatch the arr然后发送 arr

 dispatch(setDoc(arr));

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

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