I've created a typed AsyncThunk
following the instructions from the redux-toolkit docs. It consists in a method to get data from an API, without any filters and return as a typed array. The code in this AsyncThunk
also calls a function to show the loading progress of data and I'm trying to use the ThunkApi config to get a reference to dispatch and call this function.
I'm doing this way:
deviceOperations.ts
import { createAsyncThunk } from '@reduxjs/toolkit';
import { AppDispatch } from 'src/store';
import { loaderActions } from '../../../features/loader/redux';
import deviceManager from '../deviceManager';
export const getAllDevices = createAsyncThunk<
Device[],
{
dispatch: AppDispatch;
}
>('device/GET_ALL_DEVICES', async (thunkApi) => {
thunkApi.dispatch(loaderActions.start('getAllDevices'));
try {
const devices = await deviceManager.getAllDevices();
return { ...devices };
} finally {
thunkApi.dispatch(loaderActions.stop('getAllDevices'));
}
});
DevicePage.tsx
const DevicePage = () => {
const classes = useStyles();
const dispatch = useAppDispatch();
const { push } = useHistory();
useEffect(() => {
dispatch(deviceOperations.getAllDevices());
}, [dispatch]);
};
The line dispatch(deviceOperations.getAllDevices());
is causing the error Expected 1 arguments, but got 0.ts(2554) typings.d.ts(302, 701): An argument for 'arg' was not provided.
.
What is wrong with this function?