【发布时间】:2020-04-08 02:39:15
【问题描述】:
我正在尝试使用 React 和 TypeScript 设置我的 Redux 存储,但它给了我一个错误,我的 auth 减速器是 undefined。
这是我的 store.ts:
import {Action, applyMiddleware, combineReducers, compose, createStore} from 'redux';
import { auth, IAuthState } from './Auth/reducer';
import { general, IGeneralState } from './General/reducer';
export interface IAppState {
auth: IAuthState;
general: IGeneralState;
}
export const rootReducer = () => combineReducers({
auth: auth,
general: general,
});
const store = createStore<IAppState, Action<any>, {}, {}>(
rootReducer(),
(window as any).__REDUX_DEVTOOLS_EXTENSION__ &&
(window as any).__REDUX_DEVTOOLS_EXTENSION__()
);
export { store };
这是我的auth reducer:
import { User } from '../../interfaces/user.interface';
import { AuthActionTypes } from './actions';
export interface IAuthState {
user: User;
authenticated: boolean;
}
const initialState: IAuthState = {
user: null,
authenticated: true,
};
export const auth = (state: IAuthState = initialState, action: any): IAuthState => {
switch (action.type) {
case AuthActionTypes.Setuser:
const { User } = action.payload;
return {
...state,
user: User
};
case AuthActionTypes.Logout:
return {
...state,
user: null,
authenticated: false,
};
}
};
它给了我错误:
未捕获的错误:Reducer "auth" 在期间返回未定义 初始化。如果传递给减速器的状态是未定义的,你 必须显式返回初始状态。初始状态可能不是 不明确的。如果你不想为这个 reducer 设置一个值,你可以 使用 null 而不是 undefined。
【问题讨论】:
标签: reactjs typescript redux