简体   繁体   English

我如何更新redux-form中的数组字段值?

[英]How I can update array field value in redux-form?

isMain = (v, k) => {

    let {avatars: {fields}, push} = this.props;

    fields.getAll().map((avatar, key) => {

      fields.remove(key);

      push('avatars', {
        deleteImage: avatar.deleteImage || false,
        uuid: avatar.uuid,
        fileName: avatar.fileName,
        url: avatar.url,
        setMain: key === k,
      });

    });

};

This is how I try^ Doesn't work, I don't know why 这是我尝试的方法^不起作用,我不知道为什么

I use v6 我用的是v6

I also tried insert 我也试过插入

But with insert field not removed only - copied! 但是插入字段不会被删除 - 复制! Although the value of new fields changed 虽然新字段的值发生了变化

if you only want to update a single item in array avatars , an alternative solution is using redux-form's Action Creators . 如果您只想更新数组avatars的单个项目,另一种解决方案是使用redux-form的Action Creators Just need to provide the index value to action: 只需要为操作提供索引值:

import { change } from 'redux-form'
...
setMain: (k) => dispatch(change(`yourForm`, `avatars[${k}].setMain`, true))

You can create another action removeMain(k) in the same manner to remove the old main avatar item. 您可以以相同的方式创建另一个动作removeMain(k)以删除旧的主要头像项目。

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

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