Files
next.js/errors/context-in-server-component.mdx
T
74a19b7467 docs: unify the header deps by removing # (#72391)
## Summary
Remove redundant `#` from [error
pages](https://github.com/vercel/next.js/tree/canary/errors).

## Description
Follow up #52038 and #72263.
Updated
[template.txt](https://github.com/vercel/next.js/blob/canary/errors/template.txt)
and clarified the header deps.
Then rewrite as per the template.

### Improving Documentation

- [x] Run `pnpm prettier-fix` to fix formatting issues before opening
the PR.
- [x] Read the Docs Contribution Guide to ensure your contribution
follows the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

Co-authored-by: Delba de Oliveira <[email protected]>
2024-11-07 08:58:43 +00:00

33 lines
725 B
Plaintext

---
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 Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)