Javascript Redux Reducer中的Action.type未定义错误
我不知道为什么我必须检查我的减速机中是否存在Javascript Redux Reducer中的Action.type未定义错误,javascript,reactjs,redux,async-await,reducers,Javascript,Reactjs,Redux,Async Await,Reducers,我不知道为什么我必须检查我的减速机中是否存在actions。这可能是因为我们在actions/API方法中使用了asyncwait 减速器 分派操作的组件: 商店 我试过的 我注意到我的mapDispatchToProps写得有点奇怪,所以我修复了它,但是如果我删除if语句,我仍然会得到错误actions is undefined:'( 明白了!这是我的测试 it('returns expected initState', () => { let expected = {roles
actions
。这可能是因为我们在actions/API方法中使用了asyncwait
减速器
分派操作的组件:
商店
我试过的
我注意到我的mapDispatchToProps写得有点奇怪,所以我修复了它,但是如果我删除if语句,我仍然会得到错误actions is undefined
:'(
明白了!这是我的测试
it('returns expected initState', () => {
let expected = {roles: []};
let actual = partyReducer();
expect(actual).toEqual(expected);
});
^上面的测试假设是,如果没有传入任何状态,那么初始状态是否为return。但是,应该始终传入操作
修正:
it('返回预期的initState',()=>{
let expected={roles:[]};
让actual=partyReducer(未定义,{});//代码在我看来还行。你是如何在根减速器中注册减速器的?还包括thunk中间件吗?@Clarity刚刚在代码上方添加了存储代码看起来还行。但为什么在减速器中进行双重导出?奇怪的是,存储代码也还行。我可能会开始调试thunk操作,并检查它是否/是什么类型的操作事实上,这就是测试的编写方式!
import {getRoles} from '../shared/services/api';
export const Actions = {
SET_ROLES: 'SET_ROLES'
};
export const fetchRoles = () => async dispatch => {
try {
const response = await getRoles();
const roles = response.data;
dispatch({
type: Actions.SET_ROLES,
roles
});
} catch (error) {
dispatch({
type: Actions.SET_ROLES,
roles: []
});
}
};
componentDidMount() {
this.props.fetchRoles();
this.onSubmit = this.onSubmit.bind(this);
}
...
export const mapDispatchToProps = dispatch => {
return {
fetchRoles: () => {
dispatch(fetchRoles());
}
};
};
import {createStore, combineReducers, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import {reducer as formReducer} from 'redux-form';
// Reducers
import partyReducer from '../reducers/party-reducer';
export default function configureStore(initialState) {
let reducer = combineReducers({
form: formReducer,
party: partyReducer
});
let enhancements = [applyMiddleware(thunk)];
if (process.env.PROD_ENV !== 'production' && typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__) {
enhancements.push(window.__REDUX_DEVTOOLS_EXTENSION__());
}
return createStore(reducer, initialState, compose(...enhancements));
}
import {fetchRoles as fetchRolesAction} from '../../../actions/party-actions';
...
export const mapDispatchToProps = dispatch => ({
fetchRoles: () => dispatch(fetchRolesAction())
});
it('returns expected initState', () => {
let expected = {roles: []};
let actual = partyReducer();
expect(actual).toEqual(expected);
});
it('returns expected initState', () => {
let expected = {roles: []};
let actual = partyReducer(undefined, {}); // <-- undefined state, + action
expect(actual).toEqual(expected);
});