You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成RTK Query的createApi时触发「需提供全部3个hooks」编译错误的问题求助

集成RTK Query的createApi时触发「需提供全部3个hooks」编译错误的问题求助

我正在维护一个有着上百个reducer的Redux+React Native遗留项目,最近想把RTK Query集成进来提升数据请求效率。我先写了一个非常简单的测试示例,打算先在主项目里跑通,但刚引入RTK Query的createApi函数,编译就直接报错了。

报错信息如下:

Error: When using custom hooks for context, all 3 hooks need to be provided: useDispatch, useSelector, useStore. Hook useDispatch was either not provided or not a function., js engine: hermes

只要删掉createApi相关的代码,项目就能正常编译运行。我试过好几种RTKQ的实现方式,做了一些小调整,但只要一加上createApi,就会触发这个错误。

另外提一句,我还搭了一个极简的barebone React Native测试环境来专门调试RTKQ的编译问题,我知道现在还没把pokemonApi加入reducers,这已经是我能写出来的最简化示例了。

我的项目依赖(内容有截断)

"react": "18.2.0",
"expo": "...",
// 其他Redux/RTK相关依赖

测试用的极简代码

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

// 最基础的RTK Query API定义
export const pokemonApi = createApi({
  reducerPath: 'pokemonApi',
  baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
  endpoints: (builder) => ({
    getPokemonByName: builder.query({
      query: (name) => `pokemon/${name}`,
    }),
  }),
});

export const { useGetPokemonByNameQuery } = pokemonApi;

有没有大佬遇到过类似的问题?或者能帮我分析下这个错误的触发原因,以及对应的解决办法吗?

备注:内容来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 02:44:38