mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
9c757f6d5c
Closes: - https://linear.app/vercel/issue/DOC-4655/client-components - https://linear.app/vercel/issue/DOC-4656/server-components - https://linear.app/vercel/issue/DOC-4657/composition-patterns Redirects: https://github.com/vercel/front/pull/45564 This PR: - Adds new **Server and Client Components** page to **Getting Started** - Explains how Server and Client components are rendered - Clarifies when to use them - Reviews and simplifies composition patterns (examples) - Improves the **How does PPR work** section in light of static, dynamic, and streaming.
33 lines
724 B
Plaintext
33 lines
724 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/getting-started/server-and-client-components#examples)
|