8

We were using react's context API in our next.js 12 apps.

We want to upgrade to next.js 13.

We get this error:

react__WEBPACK_IMPORTED_MODULE_0__.createContext is not a function

For this code:

import React from 'react'

const SiteContext = React.createContext()

export default SiteContext

What should we do now? I can't find resources online for this.

Also, we need the Context API on the server because SEO matters and we don't want to render on the client.

5
  • try, change export default to export Commented Oct 31, 2022 at 12:11
  • @JacksonQuintero, that won't even compile. There is either deafult exports, or named exports in JS. Commented Oct 31, 2022 at 12:19
  • You can change it to a client component, as for the SEO, add a different head.js file for each page inside the app folder. It should work just as the old Head component Commented Dec 21, 2022 at 17:13
  • Does this answer your question? Retrieve data server side and save in context with Next.js Commented Sep 13, 2023 at 8:12
  • I've written a detailed answer about how to use Context alongside RSC, and also create a kind of server context using "cache" stackoverflow.com/a/75533592/5513532 Commented Sep 13, 2023 at 8:14

3 Answers 3

6

The issue is that you need the "use client" directive.

The error is being suppressed because of your import statement. Change the import to import { useContext } from 'react' and you will get the following error:

You're importing a component that needs createContext. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.

Checkout the beta docs for more details, but basically, ALL components inside the app directory are server components. For client components, you need to use the directive.

"use client"

import React from 'react'

const SiteContext = React.createContext()

export default SiteContext
Sign up to request clarification or add additional context in comments.

4 Comments

Does this mean that we can't use Context API on the server side? Is it a React limitation, or a Next limitation?
Yup, useState, useEffect and createContext are client-only features. The "use client" directive was introduced with React as part of the Server Components features, so it is a "React thing" but I'm not sure it's a limitation per se. If your component needs to support user interactions such as those that would require state, then it's a client component by definition.
yes it's a client component. But can we render it on the server for the sake of SEO? Let's say we want to have a contact us form. It's a client component. But the /contact page needs to be rendered on the server so that Google can index it.
Short answer is yes. Check out docs for the details, but yes, both Server and Client Components can be prerendered on the server at build time.
4

In app directory you create your context inside a client component. Because you will need useState,useEffect hooks. the way you crete context api is same as before. But you need to wrap top layout file with this context.

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <head />
      <body>
        <main>
          <AuthContext>
            <main>
              {children}
            </main>
          </AuthContext>
        </main>
      </body>
    </html>
  );
}

AuthContext is a client component and wraps children. Normally client components cannot render server components but since we are using children prop, components inside children tree can be server components.

the problem with context api on the app directory is any component that needs to reach context has to be a client component because it needs to use useContext hook.

3 Comments

Just to clarify, are you saying that placing any component inside of AuthContext does not automatically make it a client-side component? So if I place a component that does not useContext with AuthContext, it will still be rendered as a server component?
2

Next 13 does not support the React Context API on the server is the issue. Considering SEO matters you would have to stick with the old apps/pages approach for now. I hope they support context soon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.