2023-07-05 15:11:16 +02:00
|
|
|
---
|
|
|
|
title: createContext in a Server Component
|
|
|
|
---
|
2022-12-12 03:46:29 +01:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Why This Error Occurred
|
2022-12-12 03:46:29 +01:00
|
|
|
|
|
|
|
You are using `createContext` in a Server Component but it only works in Client Components.
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Possible Ways to Fix It
|
2022-12-12 03:46:29 +01:00
|
|
|
|
|
|
|
Mark the component using `createContext` as a Client Component by adding `'use client'` at the top of the file.
|
|
|
|
|
|
|
|
##### Before
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```jsx filename="app/example-component.js"
|
2022-12-12 03:46:29 +01:00
|
|
|
import { createContext } from 'react'
|
|
|
|
|
|
|
|
const Context = createContext()
|
|
|
|
```
|
|
|
|
|
|
|
|
##### After
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```jsx filename="app/example-component.js"
|
2022-12-12 03:46:29 +01:00
|
|
|
'use client'
|
|
|
|
import { createContext } from 'react'
|
|
|
|
|
|
|
|
const Context = createContext()
|
|
|
|
```
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Useful Links
|
2022-12-12 03:46:29 +01:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
[Server and Client Components](/docs/getting-started/react-essentials#context)
|