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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstable return reference of generated hook when using a custom mutator hook. #1359

Closed
henkerik opened this issue May 6, 2024 · 0 comments 路 Fixed by #1374
Closed

Unstable return reference of generated hook when using a custom mutator hook. #1359

henkerik opened this issue May 6, 2024 · 0 comments 路 Fixed by #1374
Assignees
Labels
bug Something isn't working tanstack-query TanStack Query related issue
Milestone

Comments

@henkerik
Copy link
Contributor

henkerik commented May 6, 2024

What are the steps to reproduce this issue?

First of all, thanks for building Orval. We use it for quite a few projects 馃憤 .

One can generate endpoints with a custom mutator hook, for example:

return (params?: ListPetsParams, signal?: AbortSignal) => {
return listPets({ url: `/pets`, method: 'GET', params, signal });
};

export const useListPetsHook = () => {
  const listPets = useCustomInstance<Pets>();

  return (params?: ListPetsParams, signal?: AbortSignal) => {
    return listPets({ url: `/pets`, method: 'GET', params, signal });
  };
};

What happens?

The reference returned by the generated useListPetsHook isn't stable: it changes on every render cycle.

What were you expecting to happen?

I would have expected the following:

export const useListPetsHook = () => {
  const listPets = useCustomInstance<Pets>();

  return useCallback((params?: ListPetsParams, signal?: AbortSignal) => {
    return listPets({ url: `/pets`, method: 'GET', params, signal });
  }, [listPets]);
};

Now, the useListPetsHook will return the same reference on subsequent calls (given that your useCustomInstance doesn't change of course).

A stable reference is useful when one uses memoization downstream, and the result of the useListPetsHook is part of a useMemo or useCallback call in user-land code.

@melloware melloware added bug Something isn't working tanstack-query TanStack Query related issue labels May 6, 2024
@henkerik henkerik changed the title Unstable return reference of generated hooks when using a custom mutator hook. Unstable return reference of generated hook when using a custom mutator hook. May 7, 2024
@melloware melloware added this to the 6.29.0 milestone May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tanstack-query TanStack Query related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants