如何在字段未注册时使用 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操作,只需确保将其值设置为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进行测试

最新更新