I started a project recently and I use redux/toolkit and its queries API to manage my data.
I tried to use the auto-generated hooks but I get an error from typescript saying they cant find them. Here’s an example of code I wrote:
export const elementsApi = api.injectEndpoints({
endpoints: (build) => ({
getElements: build.query<
{
rootParentId: string;
} & GetElementReturnType,
number
>({
query: (search) => ({ url: `Menu/GetFiles${search}` }),
transformResponse: ({ data }: { data: BackendMenuType }) => {
return {
rootParentId: data.MenuId,
...getElements(data),
};
},
providesTags: (result, _error, id) => [{ type: elements, id }],
}),
}),
});
export const { useGetElementsQuery } = elementsApi;
The error message I get when I try to get the hook :
Property 'useGetElementsQuery' does not exist on type 'Api<BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>, { getElements: QueryDefinition<number, BaseQueryFn<...>, string, { ...; } & GetElementReturnType, "api">; }, "api", string, unique symbol>'.
I made sure my VSCode was set to a version higher than 4.1+, as suggested by this issue.
It is 5.0.4
the typescript version installed in my devdependencies is also 5.0.4
Is there another thing I forgot to check?
>Solution :
You probably imported createApi from @reduxjs/toolkit/query, not @reduxjs/toolkit/query/react – so you used the non-hook version.