RTK Query createApi() endpoints not showing up as hooks in Typescript

huangapple go评论85阅读模式
英文:

RTK Query createApi() endpoints not showing up as hooks in Typescript

问题

Here's the translation of the code-related part:

尝试使用 RTK Query 进行简单的基础查询,在使用 createApi() 定义我的 API 后,当我尝试解构 API 以获取我的 hooks 时,Typescript 给我报错。

我甚至复制粘贴了文档中的 Pokémon 示例代码,但即使这样,它也说 hook 不存在。

显然,我漏掉了一些东西。

这是代码,直接从文档中提取,但返回类型是字符串:

export const pokemonApi = createApi({
    reducerPath: 'pokemonApi',
    baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
    endpoints: (builder) => ({
      getPokemonByName: builder.query<string, string>({
        query: (name) => `pokemon/${name}`,
      }),
    }),
})
  
export const { useGetPokemonByNameQuery } = pokemonApi;

useGetPokemonByNameQuery 就是显示错误的地方:

Property 'useGetPokemonByNameQuery' does not exist on type 'Api<BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>, { getPokemonByName: QueryDefinition<...> }, "pokemonApi", never, unique symbol>'.

我做错了什么?

英文:

Trying a simple base query with RTK Query and after defining my api with createApi(), Typescript is giving me error when I try to destructure the api to get my hooks.

I then copied pasted the pokemon example code from the docs and even with that, it's saying the hook doesn't exist.

Clearly I'm missing something.

Here's the code, pulled right from the docs but with a string return type:

export const pokemonApi = createApi({
    reducerPath: &#39;pokemonApi&#39;,
    baseQuery: fetchBaseQuery({ baseUrl: &#39;https://pokeapi.co/api/v2/&#39; }),
    endpoints: (builder) =&gt; ({
      getPokemonByName: builder.query&lt;string, string&gt;({
        query: (name) =&gt; `pokemon/${name}`,
      }),
    }),
})
  
export const { useGetPokemonByNameQuery } = pokemonApi;

That useGetPokemonByNameQuery is what shows the error:

Property &#39;useGetPokemonByNameQuery&#39; does not exist on type &#39;Api&lt;BaseQueryFn&lt;string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta&gt;, { getPokemonByName: QueryDefinition&lt;...&gt;; }, &quot;pokemonApi&quot;, never, unique symbol&gt;&#39;.

What am I doing wrong?

答案1

得分: 2

*@reduxjs/toolkit: 1.9.5*

来自[官方文档](https://redux-toolkit.js.org/rtk-query/api/created-api/overview#react-hooks):

&gt; RTK Query的核心createApi方法与Redux核心库和Redux Toolkit一样,是与UI无关的。它们都是纯粹的JavaScript逻辑,可以在任何地方使用。

&gt; 但是,RTK Query还提供了为每个端点自动生成React hooks的功能。由于这个功能特定依赖于React本身,RTK Query提供了一个替代入口点,其中包含这个功能。

&gt; 如果你使用了React特定版本的createApi,生成的Api片段结构也会包含一组React hooks。

&gt; 相同的hooks也被添加到Api对象本身,并根据端点名称和查询/变更类型自动生成名称。

确保从以下路径导入`createApi`函数:

```ts
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

而不是:

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query';

完整示例可以在此处找到。


<details>
<summary>英文:</summary>

*@reduxjs/toolkit: 1.9.5*

From the [/rtk-query/api/created-api/overview#react-hooks](https://redux-toolkit.js.org/rtk-query/api/created-api/overview#react-hooks) doc:

&gt; The core RTK Query createApi method is UI-agnostic, in the same way that the Redux core library and Redux Toolkit are UI-agnostic. They are all plain JS logic that can be used anywhere.

&gt; However, RTK Query also provides the ability to auto-generate React hooks for each of your endpoints. Since this specifically depends on React itself, RTK Query provides an alternate entry point that exposes a customized version of createApi that includes that functionality

&gt; If you have used the React-specific version of createApi, the generated Api slice structure will also contain a set of React hooks.

&gt; The same hooks are also added to the Api object itself, and given auto-generated names based on the endpoint name and query/mutation type.

Make sure the `createApi` function import from the below path:

```ts
import { createApi, fetchBaseQuery } from &#39;@reduxjs/toolkit/query/react&#39;;

NOT:

import { createApi, fetchBaseQuery } from &#39;@reduxjs/toolkit/query&#39;;

The full example can be found here

huangapple
  • 本文由 发表于 2023年6月6日 13:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定