【发布时间】:2017-03-12 01:12:36
【问题描述】:
文档中的示例仅显示如何使用 redux 状态更新 initialValues(请参阅 http://redux-form.com/6.0.0-alpha.4/examples/initializeFromState/)。我不想使用 redux,因为它对于我正在构建的应用程序来说真的很乱。我宁愿使用组件状态。
我尝试在 redux-form 连接器中使用异步自执行功能,但没有奏效:
ApplicationNewForm = reduxForm({
form: 'appapp',
returnRejectedSubmitPromise: true,
initialValues: (async function() { ... }()),
handleSubmit
})(ApplicationNewForm)
那没用(那应该很明显,为什么,组件不在乎分配是否是异步的...)...
我还尝试在 async componentDidMount 中进行 api 调用,并使用 this.state 设置我的 <Field /> 的 value 属性。
在每种情况下,值似乎都没有进入 redux 存储,因此不要更新字段中的值。
有什么想法吗?
编辑:
解决方案是使用来自 api 调用的数据手动调用 async componentDidMount 中的 this.props.initialize。这确实有效:
async componentDidMount() {
try {
const res = await axios.get('/my/user/api/')
const { data: user } = res
this.props.initialize({...user})
} catch(err) {}
}
【问题讨论】:
-
我想我可以在我的表单组件中使用
change属性(请参阅redux-form.com/6.5.0/docs/api/Props.md/…),当异步功能得到解决...如果可行,将更新
标签: javascript reactjs redux react-redux redux-form