【发布时间】:2018-08-29 20:06:27
【问题描述】:
我正在尝试使用某个用户(firstName、lastName、email)的已编辑字段来更新我的状态(用户列表)。这个想法是每当用户更改表单中输入的值时,都会触发创建者操作以使用新值更新状态中的用户对象。以下是我的代码:
在我的reducers.js:
case actionCreators.changeUserValues.getType():
return {
...state,
usersList: [
...state.usersList.map((user) => {
if (user.username === action.payload.username) {
return {
...user,
[action.payload.name]: action.payload.value,
};
}
return user;
}),
],
editButtonDisabled: false,
submitButtonDisabled: true,
selectedUserProfile: {
userObject: {
...state.selectedUserProfile.userObject,
[action.payload.name]: action.payload.value,
},
},
};
在actionCreators.js 文件中:
const changeUserValues = createAction(CHANGE_USER_VALUES, userData => userData);
const onValueChange = event => (dispatch, getState) => {
const { value, name } = event.target;
const { username } = getState().selectedUserProfile.username;
return dispatch(changeUserValues({ name, value, username }));
};
在我的容器中:
const mapDispatchToProps = dispatch => ({
// some stuff
onValueChange: () => dispatch(actionCreators.onValueChange()),
});
为了测试我的代码,我编辑了表单,并在onValueChange() actionCreator 代码中收到此错误TypeError: Cannot read property 'target' of undefined。我不知道如何传递/捕获event(因为我正在尝试“还原”this)
【问题讨论】:
标签: javascript reactjs forms redux react-redux