reactjs - 无法在组合减速器中减速器
问题描述
我正在尝试将 authenticationReducer 添加到组合减速器,但它给了我以下错误,
import { combineReducers } from "redux";
import { authenticationReducer } from "./authentication-reducer";
import { AuthResponse } from "../../services/authentication-service";
export interface StoreState {
authentication: AuthResponse;
}
export const rootReducer = combineReducers<StoreState>({
authentication: authenticationReducer
});
行动,
import {
authenticationService,
AuthResponse
} from "../../services/authentication-service";
import { Dispatch } from "redux";
import { AuthActionTypes } from "./types";
export interface AuthenticationAction {
type: AuthActionTypes;
payload: AuthResponse | null;
}
export const authenticate = (credentials: any) => {
return async (dispatch: Dispatch) => {
dispatch<AuthenticationAction>({
type: AuthActionTypes.AUTHENTICATING,
payload: null
});
await authenticationService
.authenticate(credentials)
.then(response => {
if (response.data.access_token) {
console.log(response);
localStorage.setItem("authResponse", JSON.stringify(response));
dispatch<AuthenticationAction>({
type: AuthActionTypes.AUTHENTICATED,
payload: response.data
});
} else {
dispatch<AuthenticationAction>({
type: AuthActionTypes.AUTHENTICATION_FAILED,
payload: null
});
}
})
.catch((error: any) => {
dispatch({
type: AuthActionTypes.AUTHENTICATION_FAILED,
payload: error
});
});
};
};
减速器,
import { AuthActionTypes } from "../actions/types";
import { AuthenticationAction } from "../actions/authentication-actions";
import { AuthResponse } from "../../services/authentication-service";
let authResponse = JSON.parse(localStorage.getItem("authResponse") || "{}");
const initialState = authResponse
? { loggedIn: true, authResponse: authResponse }
: {};
export const authenticationReducer = (
state: AuthResponse,
action: AuthenticationAction
) => {
switch (action.type) {
case AuthActionTypes.AUTHENTICATED:
return action.payload;
default:
return state;
}
};
存储配置,
import { createStore, applyMiddleware, compose } from "redux";
import { rootReducer } from "./reducers";
import thunk from "redux-thunk";
export const configureStore = () => {
return createStore(rootReducer, applyMiddleware(thunk));
};
不知道我在这里做错了什么。任何帮助表示赞赏。谢谢。
解决方案
让我们尝试将default value
状态分配reducer
给initialState
。最初它可能为空/未定义,这就是您收到错误“undefined
不可分配给AuthResponse
”的原因
export const authenticationReducer = (
state: AuthResponse = initialState, // assigning default value
action: AuthenticationAction
) => {
switch (action.type) {
case AuthActionTypes.AUTHENTICATED:
return action.payload;
default:
return state;
}
};
另一个技巧是,您可以将state
类型更改为any
. 但这不会检查您定义的类型。
推荐阅读
- laravel - 在“https://email.us-west-1.amazonaws.com”上执行“SendRawEmail”时出错;AWS HTTP laravel 5.2
- javascript - ChartJS:在工具提示中调用数组对象的属性
- reactjs - 在 React-Native 中转换回对人类友好的计时器格式
- swift - 如何让 Firestore 数据显示在标签中
- python - Python请求未返回整页内容
- java - 如何在 Apache Tomcat 8/9 中设置 HTTP Expect-CT 标头?
- sql - SQL 窗口函数以每日级别计算不同的月份日期
- reactjs - 我正在尝试将函数从 app.js 传递到详细信息屏幕。但我不断收到此错误(在导航中发现了不可序列化的值)
- java - 如何使用我的密钥使用 javax.crypto.Cipher 进行加密?
- background - 渐变透明PNG