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

infinite loop occurs on v2 #501

Closed
mehm8128 opened this issue Dec 23, 2024 · 3 comments
Closed

infinite loop occurs on v2 #501

mehm8128 opened this issue Dec 23, 2024 · 3 comments

Comments

@mehm8128
Copy link

I upgraded connect-query-es and peripheral connect tools to v2 referencing migration guide, but infinite loop occurs when we call unary method.

This is my minimal reproduction source code which I created based on the examples-es/nextjs.
https://github.com/mehm8128/connect-query-infinite-loop-repro

Please check it following the below procedures.

  1. clone reprository
  2. run npm i
  3. temporarily comment out 13 to 22 lines (the definition of res and the calling useEffect) in pages/index.tsx (or you will not be able to see the page)
  4. start server with npm run start (you will fail to run npm run build, so use start command)
  5. when you can see aaa text on the started page (http://localhost:3000 ) , revert the comment out you did in the third step
  6. you will find the infinite loop on the network tab of the devtools

Sorry if there is any my mistake, but thank you for checking my issue 🙏

@paul-sachs
Copy link
Collaborator

Hi @mehm8128. Looking through your example, I see that you create the transport on every render. connect-query now takes into account the transport when generating keys so that's likely where it's breaking. There are a few ways to fix this:

  1. Move the transport creation to the module level.
const baseTransport: ConnectTransportOptions = {
  baseUrl: "/api",
  useBinaryFormat: false,
};
const transport = createConnectTransport(baseTransport)
export const useGrpcClient = () => {
  ...
  1. Add a special key to the transport so it can be serialized as a key.
const transport = addStaticKeyToTransport(createConnectTransport(baseTransport), 'grpc-transport'));

Generally, I'd recommend the option 1, since there's no magic string, but it depends on if your transport needs some runtime data. In this example, I don't see anything that would indicate it needed access to runtime data so this is the best option. Option 2 on the other hand, can be used to define exactly what you want to key to be. This means that if you want to trigger queries when something about the transport changes, you need to make sure that grpc-transport string changes along with it (doesn't matter what it is, as long as it changes).

@mehm8128
Copy link
Author

Thank you so much!!!
Because my product uses runtime data to create transport unlike repro code, I decided to choose the second option at this time and it works well without occurring infinite loop!
I'll close this issue as solved 😄

@paul-sachs
Copy link
Collaborator

I realized there's another option if the static key method is a little restrictive: You can memoize the transport using useMemo from react. Depends on the use case really. addStaticKeyToTransport works for SSR as well where useMemo does not. Glad it worked though.

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