简体   繁体   中英

Redux Thunk Typescript

I'm trying to make a thunk that filters a base array and passes data into the filtered array, but I keep getting an error when I do a dispatch in useEffect.

Writes: "Argument of type '(dispatch: ThunkDispatch<IRootState, void, Action>, getState: IRootState) => void' is not assignable to parameter of type 'AnyAction'."

I don't understand what to do, I followed the redux documentation, but still nothing works

Thunk action-creator:

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));
  };
};

Action interfaces

export interface IFilteringServices {
  type: ActionType.FILTERING_SERVICES;
  payload: IService[];
}


export type Action =
  | IGetServices
  | IAddToCart
  | IRemoveFromCart
  | IFilteringServices
  | IUseFilters;

RootState interface:

export type IRootState = typeof storeState;

dispatch

useEffect(() => {
    dispatch(filterServices());
  }, []);

I changed the dispach hook and everything started working:

export const useAppDispatch = () =>
  useDispatch<ThunkDispatch<IRootState, unknown, AnyAction>>();

Also changed the getState call, I forgot it was a function:

(dispatch: Dispatch<Action>, getState: () => IRootState) => {

const state = getState().services.filteredServices

...other code
}


I changed this code with filtering, but maybe the information about the hook and getState will help someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM