Back to Next Js

createContext in a Server Component

errors/context-in-server-component.mdx

16.2.5671 B
Original Source

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

jsx
import { createContext } from 'react'

const Context = createContext()

After

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

const Context = createContext()