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

Detected multiple renderers concurrently rendering the same context provider #119

Open
jamesfrye420 opened this issue Jul 11, 2022 · 3 comments

Comments

@jamesfrye420
Copy link

I get the following warning when using this as hoc inside my Next.js app

My Apollo client:

import { ApolloClient, InMemoryCache } from '@apollo/client';
import { NextPageContext } from 'next';
import { withApollo } from 'next-apollo';
import { PaginatedPosts } from '../generated/graphql';

const createClient = (ctx: NextPageContext) =>
  new ApolloClient({
    uri: 'http://localhost:4000/graphql',
    headers: {
      cookie: (typeof window === 'undefined' && ctx?.req?.headers.cookie) || '',
    },
    cache: new InMemoryCache({
      typePolicies: {
        Query: {
          fields: {
            posts: {
              keyArgs: [],
              merge(existing: PaginatedPosts | undefined, incoming: PaginatedPosts): PaginatedPosts {
                return {
                  ...incoming,
                  posts: [...(existing?.posts || []), ...incoming.posts],
                };
              },
            },
          },
        },
      },
    }),
    credentials: 'include',
  });

export default withApollo(createClient);

and this is how I am using it inside my pages

import { Box, Button, Flex, Heading, Link } from '@chakra-ui/react';
import { NextPage } from 'next';
import NextLink from 'next/link';
import { useRouter } from 'next/router';
import IndexHeader from '../components/IndexHeader';
import Layout from '../components/Layout';
import PostStack from '../components/PostStack';
import { PaginatedPosts, usePostsQuery } from '../generated/graphql';
import withApollo from '../utils/withApollo';

const Index: NextPage = () => {
  const router = useRouter();
  const { data, loading, fetchMore, variables } = usePostsQuery({
    variables: {
      limit: 15,
      cursor: null,
    },
    notifyOnNetworkStatusChange: true,
  });

  const fetchMoreVar = () => ({
    variables: {
      limit: variables?.limit,
      cursor: data?.posts.posts[data?.posts.posts.length - 1]?.createdAt,
    },
  });

  return (
    <Layout>
      <IndexHeader />
      <PostStack
        data={data?.posts as PaginatedPosts | undefined}
        fetchMore={fetchMore}
        fetchMoreVar={fetchMoreVar}
        loading={loading}
        variables={variables}
      />
    </Layout>
  );
};
export default withApollo({ ssr: true })(Index);

on rendering this my console spits out the following warning

Warning: Detected multiple renderers concurrently rendering the same context provider. This is currently unsupported.

This warning only shows up in the pages in which ssr is set to true

@jonathanmorris180
Copy link

jonathanmorris180 commented Jul 24, 2022

@jamesfrye420 Did you find fix for this? I'm getting the same warning message with a very similar setup:

import { ApolloClient, InMemoryCache } from "@apollo/client";
import { NextPageContext } from "next";
import { withApollo as createWithApollo } from "next-apollo";

const createClient = (ctx?: NextPageContext) =>
  new ApolloClient({
    uri: process.env.NEXT_PUBLIC_API_URL as string,
    connectToDevTools: true,
    credentials: "include",
    headers: {
      cookie:
        (typeof window === "undefined"
          ? ctx?.req?.headers.cookie
          : undefined) || "",
    },
    cache: new InMemoryCache(),
  });

export const withApollo = createWithApollo(createClient);

Warning message:

Warning: Detected multiple renderers concurrently rendering the same context provider. This is currently unsupported.

Using:

{
  "dependencies": {
    ...
    "next": "^12.2.3",
    "next-apollo": "^5.0.8"
    ...
  }
}

@jonathanmorris180
Copy link

For others encountering this, it looks like this might be due to using React 18. next-with-apollo users seem to also have this issue after upgrading (I just upgraded to v18 yesterday).

@jamesfrye420
Copy link
Author

@jonathanmorris180 @jonathanmorris180 check this out, see if this could be useful in your case.

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