rsnext/errors/context-in-server-component.mdx
Delba de Oliveira 44d1a1cb15
docs: Migrate error messages to MDX and App Router. (#52038)
This PR is part of a larger effort to migrate error messages to MDX and
use App Router: https://github.com/vercel/front/pull/23459
2023-07-05 06:11:16 -07:00

32 lines
688 B
Text

---
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
```jsx filename="app/example-component.js"
import { createContext } from 'react'
const Context = createContext()
```
##### After
```jsx filename="app/example-component.js"
'use client'
import { createContext } from 'react'
const Context = createContext()
```
## Useful Links
[Server and Client Components](/docs/getting-started/react-essentials#context)