问题描述
我学会了反应并尝试使Redux正常工作,所以我使用了Redux-logger。从App.js分派两个动作时,它作为顶部图像显示为“ ALBUME_DATA_LOADED”。
我不确定我是否发送了“对象,对象”操作,我放置了断点和控制台日志,而奇怪的是,react-logger捕获了我认为不是我发送的操作。
有什么主意吗?
以下是我在以下代码中使用的操作类型,如userActionTypes
:
文件user.types.js:
export const userActionTypes = {
SAVE_USER_START: 'SAVE_USER_START',SAVE_USER_SUCCESS: 'SAVE_USER_SUCCESS',SAVE_USER_FAILURE: 'SAVE_USER_FAILURE',};
这是动作:
文件user.actions.js;
import { userActionTypes } from './user.types';
import { withFirebase } from '../../firebase';
import * as ROLES from '../../constants/roles';
const saveUserStart = () => ({
type: userActionTypes.SAVE_USER_START,});
const saveUserSuccess = user => ({
type: userActionTypes.SAVE_USER_SUCCESS,payload: user,});
const saveUserFailure = errMsg => ({
type: userActionTypes.SAVE_USER_FAILURE,payload: errMsg,});
const asyncSaveUser = ({ firestore }) => {
return async dispatch => {
const userRef = firestore.userDoc(firestore.auth.currentUser.uid);
dispatch(saveUserStart());
firestore.db
.runTransaction(transaction => {
// This code may get re-run multiple times if there are conflicts.
return transaction.get(userRef).then(doc => {
if (!doc.exists) {
return Promise.reject('Transaction Failed: User dont exist!');
}
const newRoles = doc.data().roles;
// new roll
newRoles.push(ROLES.USER);
// remove roll
newRoles.splice(newRoles.indexOf('ANONYMOUS'),1);
// save it back
transaction.update(userRef,{ roles: newRoles });
return newRoles;
});
})
.then(newRoles => {
dispatch(saveUserSuccess());
console.log(`Transaction successfully committed role(s): ${newRoles}`);
})
.catch(error => {
dispatch(saveUserFailure(error));
console.log(error);
});
};
};
export default withFirebase(asyncSaveUser);
解决方法
在dispatch saveUserSuccess()中,您不能传递newRoles。
dispatch(saveUserSuccess(newRoles));
原因是您的mapDispatchToProps。
const mapDispatchToProps = dispatch => ({
saveUser: () => dispatch(asyncSaveUser())
});
asyncSaveUser()
不是动作创建者。