当 Field 未注册到 redux-from 时如何删除值

How to delete value when a Field is unregistred with redux-from

我有一个关于 redux-from 的问题。

当我卸载组件时,动作创建器 unRegistredField 会自动启动。问题是该值保留在商店中。

你们有没有在字段未注册时自动删除值的解决方案?

我不想手动启动动作创建器!

您可以使用 this.props.change("fieldName", null)
更新值 您可以在此处阅读此方法的文档:https://redux-form.com/6.2.0/docs/api/props.md/#-change-field-string-value-any-function-

如果有人来这里寻找从表单状态中删除值的方法(而不是仅仅更改值),您可以使用 autofill 而不是 change 操作,只需 make确保将其值设置为 undefined.

像这样:

// CustomFormContainer.jsx
import {
    reduxForm,
    change,  // Saves the value to the field.
    autofill // Saves the value to the field and sets its autofilled property to true.
} from 'redux-form'

// The presentational component
import CustomForm from './CustomForm.jsx'

const form = 'your-form-name'
const fields = [ 'field-foo', 'field-bar', 'field-baz' ]

const CustomFormContainer = reduxForm({
    form,
    fields
},
/* mapStateToProps = */ undefined,
/* mapDispatchToProps = */ function(dispatch) {
    return {
        // This will be passed as a property to the presentational component
        changeFieldValue: function(field, value) {
            dispatch(autofill(form, field, value))
        }
    }
})(CustomForm)

然后,在./CustomForm.jsx文件中,你可以做:

this.props.changeFieldValue('field-baz', undefined)

使用 redux 形式测试 v8.3.1