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 re-renders from useSerializablePreloadedQuery #288

Open
Dig-Doug opened this issue Mar 11, 2023 · 0 comments
Open

Infinite re-renders from useSerializablePreloadedQuery #288

Dig-Doug opened this issue Mar 11, 2023 · 0 comments

Comments

@Dig-Doug
Copy link

I ran into an issue with NextJS v13 useSerializablePreloadedQuery example which caused react to throw an error due to too many renders.

I have a setup similar to MainViewClientComponent, but I have:

  const [state, setState] = useState(null);
  const environment = useRelayEnvironment();
  const queryRef = useSerializablePreloadedQuery(
      environment,
      props.preloadedQuery
  );
  const [updatedQueryRef, loadQuery] = useQueryLoader(MyQuery, queryRef);
  
  return <>
    <Child queryRef={updateQueryRef} />
    <OtherChild setState={setState} />
  </>

My intention is to load the query from the server initially and when the user clicks a button load a new one. The state is not used the query logic.

The error occurs when the setState() function is called. I think what's happening is when the setState triggers a re-render, the useQueryLoader inputs also change and cause a second re-render. The second re-render causes React to detect an infinite render loop, throwing an error.

In my project, I have fixed (maybe? - I'm not a React+Relay expert) this issue by wrapping the return value in the useMemo call like so:

  return useMemo(() => {
    writePreloadedQueryToCache(preloadQuery);

    return {
      environment,
      fetchKey: preloadQuery.params.id ?? preloadQuery.params.cacheID,
      fetchPolicy,
      isDisposed: false,
      name: preloadQuery.params.name,
      kind: "PreloadedQuery",
      variables: preloadQuery.variables,
      dispose: () => {
        return;
      },
    };
  }, [preloadQuery]);

Is that the correct approach?

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

1 participant