Skip to content
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

added with-react-query-typescript example #27271

Closed
wants to merge 2 commits into from

Conversation

jashwant
Copy link

React Query Typescript Next.js example

Added a react-query example in typescript.

@ijjk ijjk added the examples Issue/PR related to examples label Jul 18, 2021
UseQueryResult,
} from "react-query";

export const api = async<T>(resource: string): Promise<T>=> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the TPost here.

export const api = async (resource: string): Promise<TPost>=> {

In usePosts I would do the same (when using explicit return we don't rely on useQuery inference).

export const usePosts = (key: any, url: string, isRouterReady: boolean): UseQueryResult<TPost, unknown> => {

With this change, consumers does not have to decide which type to set

const { isLoading, isError, data } = usePosts(

Copy link
Author

@jashwant jashwant Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api is a generic function. It can be used for any type.

In real world,api should have been in lib directory or something. But for simplicity, I kept it inside usePosts.

Also, I am using same usePost hook for both TPost and TPost[] type. ( pages/index.tsx uses the same usePost)

But if it is complicating things for consumers, I can remove it by creating different hooks for index.tsx and [id].tsx.

How do I send changes ? Just send another pull request?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So generic api make sense yes.

For the usePost, I would create two versions with proper parameters and return types (remove key:any at the same time).

I would find it more explicit when using them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested, removed generic types from usePosts.

@balazsorban44
Copy link
Member

Addressed in #38101

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants