【发布时间】:2020-07-15 10:14:24
【问题描述】:
我现在是 typescript 的新手,我正在尝试将它与 redux 操作和 reducer 结合起来。我现在遇到一个错误,我想要重构代码的最佳方法。这是我的方法,我需要帮助。
动作
import Axios from "axios";
export const fetchTODO = term => async dispatch => {
dispatch({ type: "FETCH_TODO" });
try {
const response = await Axios.get(`/api/TODO/${term}`, {});
dispatch({
type: "FETCH_TODO_SUCCESS",
payload: response.data
});
} catch (error) {
dispatch({ type: "FETCH_TODO_FAILURE", error });
}
};
减速器
const initialState = {
payload: [],
isLoading: false,
error: {}
};
export default (state = initialState, { type, payload, error }) => {
switch (type) {
case "FETCH_TODO":
return { ...state, isLoading: true };
case "FETCH_TODO_SUCCESS":
return { ...state, payload, isLoading: false };
case "FETCH_TODO_FAILURE":
return { ...state, error: error, isLoading: false };
default:
return state;
}
};
打字稿代码
types.tc
export enum ActionTypes {
fetchTodos,
fetchTodosSuccess,
fetchTodosFailure
}
动作
import { ActionTypes } from "./types";
import Axios from "axios";
import { Dispatch } from "redux";
export interface Todo {
id: number;
title: string;
completed: boolean;
}
export interface FetchTodoAction {
type: ActionTypes;
payload?: Todo[];
error?: object;
}
export const fetchTransaction = (term: string) => async (
dispatch: Dispatch
) => {
dispatch({ type: ActionTypes.fetchTodos });
try {
const response = await Axios.get<Todo[]>(
`https://jsonplaceholder.typicode.com/todos/`
);
dispatch<FetchTodoAction>({
type: ActionTypes.fetchTodosSuccess,
payload: response.data
});
} catch (error) {
dispatch({ type: ActionTypes.fetchTodosFailure, error });
}
};
StateInterface 对象
export interface StateInterface {
payload?: Todo[];
isLoading: boolean;
error?: object;
}
减速器
import { Todo, FetchTodoAction } from "./../actions/index";
import { ActionTypes } from "../actions/types";
import { StateInterface } from ".";
const initialState = {
payload: [],
isLoading: false,
error: {}
};
export const todosReducer = (
state: StateInterface = initialState,
{ type, payload, error }: FetchTodoAction
) => {
switch (type) {
case ActionTypes.fetchTodos:
return { ...state, isLoading: true };
case ActionTypes.fetchTodosSuccess:
return { ...state, payload, isLoading: false };
case ActionTypes.fetchTodosFailure:
return { ...state, error: error, isLoading: false };
default:
return state;
}
};
我在代码之后得到了这个错误,任何人都可以告诉我最好的实现
(alias) const todosReducer: (state: StateInterface | undefined, { type, payload, error }: FetchTodoAction) => StateInterface
import todosReducer
No overload matches this call.
Overload 1 of 3, '(reducers: ReducersMapObject<StateInterface, any>): Reducer<CombinedState<StateInterface>, AnyAction>', gave the following error.
Argument of type '{ todos: (state: StateInterface | undefined, { type, payload, error }: FetchTodoAction) => StateInterface; }' is not assignable to parameter of type 'ReducersMapObject<StateInterface, any>'.
Object literal may only specify known properties, and 'todos' does not exist in type 'ReducersMapObject<StateInterface, any>'.
拜托,我真的很期待得到一些支持,并提前感谢您
【问题讨论】:
标签: reactjs typescript redux action reducers