我正在尝试创建一个thunk,它过滤一个基本数组并将数据传递到筛选后的数组中,但是当我在useEffect中执行调度时,我总是会收到一个错误。
写到:“类型的参数”(分派: ThunkDispatch,getState: IRootState) => void‘不能分配给’AnyAction‘类型的参数。
我不知道该怎么做,我遵循了redux文档,但仍然没有工作。
Thunk动作-创建者:
export const filterServices = () => {
return (
dispatch: ThunkDispatch<IRootState, void, Action>,
getState: IRootState
) => {
const category = getState.filters.category;
const subcategory = getState.filters.subcategory;
let result = getState.services.services;
result =
category === 'All categories'
? result
: result.map((e: any) => e.category === category);
result =
subcategory === 'All Subcategories'
? result
: result.map((e: any) => e.subcategory === subcategory);
dispatch(getfilteringServices(result));
};
};操作接口
export interface IFilteringServices {
type: ActionType.FILTERING_SERVICES;
payload: IService[];
}
export type Action =
| IGetServices
| IAddToCart
| IRemoveFromCart
| IFilteringServices
| IUseFilters;RootState接口:
export type IRootState = typeof storeState;调度
useEffect(() => {
dispatch(filterServices());
}, []);发布于 2022-06-10 08:04:09
,我改变了分离钩子,一切都开始工作了:
export const useAppDispatch = () =>
useDispatch<ThunkDispatch<IRootState, unknown, AnyAction>>();也更改了getState调用,我忘了它是一个函数:
(dispatch: Dispatch<Action>, getState: () => IRootState) => {
const state = getState().services.filteredServices
...other code
}我用过滤方法更改了这段代码,但是关于钩子和getState的信息可能会帮助某人。
https://stackoverflow.com/questions/72560877
复制相似问题