redux只返回初始状态?



不知道为什么不工作。减速器只返回初始状态值。减速机:


import { Actions, actionTypes, todoData } from "../actions/actionTypes";
interface interfaceState {
loading: boolean;
todos?: todoData[];
err?: String;
}
let initialState: interfaceState = {
loading: false,
todos: [],
};
export const reducer = (
state: interfaceState = initialState,
action: Actions
): interfaceState => {
switch (action.type) {
case actionTypes.FETCHING_LOADING:
return {
...state,
loading: true,
};
case actionTypes.FETCHING_SUCCESS:
return {
...state,
todos: action.payload,
};
case actionTypes.FETCHING_FAIL:
return {
...state,
err: action.payload,
};
default:
return state;
}
};

操作
import { Dispatch } from "redux";
import axios from "axios";
import { actionTypes, Actions } from "./actionTypes";
export const fetchData = () => async (dispatch: Dispatch<Actions>) => {
dispatch({ type: actionTypes.FETCHING_LOADING });
try {
const data = await axios.get("http://localhost:5000/read");
dispatch({
type: actionTypes.FETCHING_SUCCESS,
payload: data.data,
});
console.log(data);
} catch (err) {
dispatch({
type: actionTypes.FETCHING_FAIL,
payload: err,
});
}
};

操作类型
export enum actionTypes {
FETCHING_LOADING = "FETCHING_LOADING",
FETCHING_SUCCESS = "FETCHING_SUCCESS",
FETCHING_FAIL = "FETCHING_FAIL",
}
type actionLoading = {
type: actionTypes.FETCHING_LOADING;
};
type actionSuccess = {
type: actionTypes.FETCHING_SUCCESS;
payload: todoData[];
};
type actionFail = {
type: actionTypes.FETCHING_FAIL;
payload: string;
};
export interface todoData {
todo_items: string;
_id: string;
}
export type Actions = actionLoading | actionSuccess | actionFail;

尝试删除": action ">

最终代码:

import { Actions, actionTypes, todoData } from "../actions/actionTypes";
interface interfaceState {
loading: boolean;
todos?: todoData[];
err?: String;
}
let initialState: interfaceState = {
loading: false,
todos: [],
};
export const reducer = (
state: interfaceState = initialState,
action
): interfaceState => {
switch (action.type) {
case actionTypes.FETCHING_LOADING:
return {
...state,
loading: true,
};
case actionTypes.FETCHING_SUCCESS:
return {
...state,
todos: action.payload,
};
case actionTypes.FETCHING_FAIL:
return {
...state,
err: action.payload,
};
default:
return state;

最新更新