我正在尝试将我的个人资料状态转换为 redux。表单和动作都正常工作,但动作的有效负载在到达减速器时是未定义的。很确定这是一个菜鸟的错误,但我一辈子都看不到它。
我按照 Stephen Grider 的 udemy 课程作为模板,并让他的帖子部分使用与登录完全相同的模式工作。redux-promise 在中间件中正确连接。
package.json(部分)
"react": "^16.2.0",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-form": "^7.2.3",
"redux-forms": "^1.0.0-3",
"redux-promise": "^0.5.3",
登录组件:
function mapStateToProps(state){
return {
profile:state.profile
};
}
export default reduxForm({
validate,
form:'PostsNewForm'
})(
connect(mapStateToProps,{login})(Login)
);
动作简介
export const profileActions = {
login:'uaLogin',
logout:'uaLogout',
register:'uaRegister',
getAll:'uaGetAll',
getOne:'uaGetOne',
delete: 'uaDelete'
};
const pa=profileActions;
export function login(values, callback){
const request=axios.post(`/api/authenticate`,values)
.then ((res)=> {
console.log ('*** action:',res.data);//res.data correctly appears
callback()
});
return {
type: pa.login,
payload:request
}
}
减速机型材
import {profileActions as pa} from '../actions';
let profile = JSON.parse(localStorage.getItem('profile'));
const initialState = profile ? { loggedIn: true, profile } : {};
export default function authentication(state = initialState, action) {
switch (action.type) {
case pa.login:
console.log('***reducer',action.payload.data);//action.payload is undefined
return {
action.payload.data
};
default:
return state
}
}