I have a hook like this
import { PayloadAction } from '@reduxjs/toolkit';
import { useEffect, useState } from 'react';
export const useOptimistic = ({
initialState,
createAction,
deleteAction,
fulfilledCallback,
}: {
initialState: boolean;
createAction: () => Promise<PayloadAction>;
deleteAction: () => Promise<PayloadAction>;
fulfilledCallback?: () => void;
}) => {
const [isAction, setIsAction] = useState<boolean>(initialState);
const [isActionDisabled, setIsActionDisabled] = useState<boolean>(false);
const handleAction = () => {
setIsActionDisabled(true);
(isAction ? deleteAction() : createAction())
.then(() => {
fulfilledCallback && fulfilledCallback();
setIsAction((prev) => !prev);
})
.finally(() => {
setIsActionDisabled(false);
});
};
useEffect(() => {
setIsAction(initialState);
}, [initialState]);
return {
isAction,
isActionDisabled,
handleAction,
};
};
And i’m trying to pass this function () => dispatch(createAttendanceEvent(eventId)) to createAction argument
const {
isAction: isAttended,
isActionDisabled: isAttendenceDisabled,
handleAction: handleRespond,
} = useOptimistic({
initialState: isInitialAttendence,
createAction: () => dispatch(createAttendanceEvent(eventId)),
deleteAction: () => dispatch(cancelAttendanceEvent(eventId)),
fulfilledCallback: () => {
dispatch(isInitialAttendence ? removeAttendenceId(eventId) : addAttendenceId(eventId));
if (!isInitialAttendence) notify('success', t('titles.responseSent'));
},
});
But i get an error
Type 'Promise<PayloadAction<unknown, string, { arg: number; requestId: string; requestStatus: "fulfilled"; }, never>
| PayloadAction<unknown, string, { arg: number; requestId: string; requestStatus: "rejected"; aborted: boolean; condition: boolean; } & ({ ...; }
| ({ ...; } & {})), SerializedError>> & { ...; } & { ...; }' is not assignable to type 'Promise<{ payload: void; type: string; }>'
What type of argument should i use to be able to pass this () => dispatch(createAttendanceEvent(eventId))