context-in-server-component.mdx


title: createContext in a Server Component

Why This Error Occurred

You are using createContext in a Server Component but it only works in Client Components.

Possible Ways to Fix It

Mark the component using createContext as a Client Component by adding 'use client' at the top of the file.

Before

import { createContext } from 'react'

const Context = createContext()

After

'use client'
import { createContext } from 'react'

const Context = createContext()

Rendered from the public repository vercel/next.js on GitHub; content belongs to its authors. Cached for up to an hour.

errors/context-in-server-component.mdx · vercel/next.js | Docs MD