我正在尝试使用我的reducer更改我的应用的状态:
const initialState = [
{
id: 0,
language: '',
session: '',
values: '',
accessCode: '',
age: 0,
gender: '',
ethnicity: '',
drinkOften: '',
drinkConcern: '',
},
];
export default function UserDetails(state = initialState, action) {
switch (action.type) {
case ADD_DETAILS:
return {
...state,
[action.field]: action.value,
};
default:
return state;
}
}
我正在调度需要更新的字段和值。
export const addDetails = (value, field) => ({
type: types.ADD_DETAILS,
value,
field,
});
在我的组件中,我将在此处发送操作:
const language = e.target.value;
this.props.actions.addDetails(language, 'language');
如果使用react-router
this.context.router.transitionTo('/');