问题描述
我无法使用操作创建器更新我的商店值。我通常从组件中获得所有需要的值,但不能用它们替换初始状态值。我相信我犯了一个愚蠢的错误,甚至拼错了一些东西,但我已经花了很多时间,浏览器中的控制台仍然向我显示该减速器状态下的空字符串。
let initialstate = {
login: "",password: ""
}
const formReducer = (state = initialstate,action) => {
switch (action.type) {
case 'SET-FORM-DATA': {
return {
...state,login: action.login,password: action.password
}
}
default: return state;
}
}
export const SetFormData = (login,password) => ({
type: 'SET-FORM-DATA',login,password
})
export default formReducer;
那个 formReducer 通常在 redux-store 中赋值
import { applyMiddleware,combineReducers,createStore } from 'redux';
import authReducer from './auth-reducer';
import dialogsReducer from './dialogs-reducer';
import profileReducer from './profile-reducer';
import usersReducer from './users-reducer';
import thunkMiddleware from 'redux-thunk';
**import formReducer from './form-reducer';**
let reducers = combineReducers({
profilePage: profileReducer,dialogPage: dialogsReducer,usersPage: usersReducer,auth: authReducer,form: formReducer
})
let store = createStore(reducers,applyMiddleware(thunkMiddleware));
window.store = store;
export default store;
我在提交表单时调用这个动作创建者:
const onSubmit = formData => {
props.SetFormData(formData.Login,formData.password)
}
这个电话似乎是,幸运的是工作正常
解决方法
我是巴西人,因此我会说葡萄牙语,但我会使用翻译来帮助您。
我不太明白这个问题,但要处理 redux,我们必须使用 dispatch
。您没有使用它,您只是在传递 redux 值。对不起,如果我弄错了,但我使用的是这样的:
import { createTypes } from "reduxsauce";
export const gradesTypes = createTypes(
`
SET_GRADES
RESET_STATE
`,{ prefix: "GRADES/" }
);
const setGrades = (grades) => {
return (dispatch) => dispatch({ type: gradesTypes.SET_GRADES,grades });
};
const resetState = () => {
return (dispatch) => dispatch({ type: gradesTypes.RESET_STATE });
};
export const gradesActions = {
setGrades,resetState,};