我需要更新一些表单数据,给定所做的选择。
我想使用redux表单中的change((函数在select字段的onChange处理程序中执行此操作。
有没有一种方法可以使用标准的react admin轻松访问redux表单更改功能?
我试着从道具上把它捡起来,但它不在那里;
const { change } = this.props;
console.log(change); // undefined
我不确定这是否是您想要的,但这是我在react管理项目中处理它的方式。我只是直接从redux-form
导入change
,并将react-admin
中的REDUX_FORM_NAME
常量传递给它。
进口
import { change } from 'redux-form'
import { REDUX_FORM_NAME } from 'react-admin'
import { connect } from 'react-redux'
编辑组件
<Edit>
<SimpleForm>
<MyCustomComponent />
</SimpleForm>
</Edit>
CCD_ 5应当自动地从CCD_ 7接收CCD_ 6道具。此外,我将其连接到redux存储:
export default connect(mapStateToProps)(MyCustomComponent)
我故意不向connect
提供第二个参数,这样我就可以访问组件中的dispatch
。也许另一种方式是可能的,但这对我来说是有效的
现在,在MyCustomComponent
中,在我为Cancel按钮创建的handleCancel
处理程序中,我更改了想要更改的字段。在我的特定情况下,我想重置字段以包含该字段未修改的存储值。
handleCancel = () => {
const { record, dispatch } = this.props;
dispatch(change(REDUX_FORM_NAME, 'name', record.name));
dispatch(change(REDUX_FORM_NAME, 'description', record.description));
dispatch(closeDetails()); // unrelated to this answer; closes a modal
}
在上面的代码中,我将"name"one_answers"description"字段重置为它们的原始值。希望这能有所帮助。