reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.59k stars 1.14k forks source link

@rtk-query/codegen-openapi include useQueryState hooks in generated code #4193

Open codeart1st opened 5 months ago

codeart1st commented 5 months ago

Currently, @rtk-query/codegen-openapi generates named export hooks for queries and mutations, but it is missing the useQueryState hooks. I would like to propose an enhancement to include the useQueryState hooks in the generated code. This addition would provide users with convenient access to the useQueryState hooks alongside the existing generated hooks. The useQueryState hooks are a great addition for react router v6 loader strategy. api.endpoints.methodName.useQueryState ist hard to type.

Steps to Reproduce:

  1. Generate code using @rtk-query/codegen-openapi.
  2. Observe that useQueryState hooks are not included in the generated code.

Expected Behavior:

The generated code should include the useQueryState hooks for each corresponding query.

codeart1st commented 5 months ago

See https://github.com/reduxjs/redux-toolkit/discussions/2751#discussioncomment-8445186

codeart1st commented 5 months ago

After some discussion this is still useful to get rid of double handled error requests from React Router v6 Loader und UI Components.

// skip fetch, if router loader already finished with an error
const { isError } = fancyApi.endpoints.fancyMethod.useQueryState(args)
const { data, isLoading } = useFancyMethodQuery(isError ? skipToken : args)