Download next.js/errors/context-in-server-component.mdx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 724 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/next.js/errors/context-in-server-component.mdx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/next.js/errors/context-in-server-component.mdx
-
curl -L -o context-in-server-component.mdx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/next.js/errors/context-in-server-component.mdx
724 Bytes
| 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) | |