【发布时间】:2016-04-24 23:21:19
【问题描述】:
我的应用商店有一个 store.authState 子树。在这个子树中,有三个东西,一个authToken,一个isFetching 布尔值,最重要的是一个fields 对象。我的表单包含两个字段:username 和 password。
我创建了一个名为 SET_FORM_FIELD_VALUE 的操作,它应该在用户更改每个字段的状态时生成和更新它们。
我希望我的SET_FORM_FIELD_VALUE 更新fields 对象。如果用户正常填写username 和password 字段,我的store.authState 应该如下所示:
{
authToken: undefined,
isFetching: false,
fields: {
username: "Brachamul",
password: "azerty123"
}
}
但是,似乎我当前的代码实际上只是覆盖了所有内容,我最终得到:
{
field: {
username: "Brachamul"
}
}
字段中的数据会根据我上次编辑的字段而变化。它是用户名或密码。
这是我的代码:
switch (action.type) {
case 'SET_FORM_FIELD_VALUE':
let field = {} // create a "field" object that will represent the current field
field[action.fieldName] = action.fieldValue // give it the name and value of the current field
return { ...state.fields, field }
如何更改它以解决我的问题?
【问题讨论】:
标签: forms reactjs redux reducers