Skip to content

Create react hook from base api #4964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
banx9x opened this issue May 5, 2025 · 1 comment
Open

Create react hook from base api #4964

banx9x opened this issue May 5, 2025 · 1 comment

Comments

@banx9x
Copy link

banx9x commented May 5, 2025

I’m building a shared API layer using @reduxjs/toolkit/query (non-react) in a separate library, so I can reuse it across multiple platforms like CLI, native apps, and React.

Here’s a example:

// core/wttr.api.ts (shared lib)
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query';

export const wttrApi = createApi({
  reducerPath: 'wttrApi',
  baseQuery: fetchBaseQuery({ baseUrl: 'https://wttr.in' }),
  endpoints: (builder) => ({
    getWeather: builder.query<any, string>({
      query: (city) => `/${city}?format=j1`,
    }),
  }),
});

This works well in core code, but when I use it in a React app, there are no hooks like useGetWeatherQuery.

❓ What I’m trying to achieve

I want to keep the core API definition in a reusable package, but still be able to generate React hooks when needed in my React apps, without redefining the endpoints.

Is there a way to wrap or “extend” this core API into one that includes hooks?

How do I properly reuse the endpoints and baseQuery from the original wttrApi?

Any guidance or examples would be greatly appreciated!

Thanks 🙏

@EskiMojo14
Copy link
Collaborator

it's an idea we've briefly touched on a couple times before (and made a draft PR here #4128) but it never solidified. if we want it i suspect that PR will need redoing, depending on the complexity of the conflicts it's got

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants