2023-07-05 15:11:16 +02:00
|
|
|
---
|
|
|
|
title: React client hook in Server Component
|
|
|
|
---
|
2023-01-04 12:01:50 +01:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Why This Error Occurred
|
2023-01-04 12:01:50 +01:00
|
|
|
|
|
|
|
You are using a React client hook in a Server Component.
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Possible Ways to Fix It
|
2023-01-04 12:01:50 +01:00
|
|
|
|
|
|
|
Mark the component using the hook 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.js"
|
2023-01-04 12:01:50 +01:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
export default function Example() {
|
2023-01-04 12:01:50 +01:00
|
|
|
useEffect(() => {
|
|
|
|
console.log('in useEffect')
|
|
|
|
})
|
|
|
|
return <p>Hello world</p>
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
##### After
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```jsx filename="app/example.js"
|
2023-01-04 12:01:50 +01:00
|
|
|
'use client'
|
2023-07-05 15:11:16 +02:00
|
|
|
|
2023-01-04 12:01:50 +01:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
export default function Example() {
|
2023-01-04 12:01:50 +01:00
|
|
|
useEffect(() => {
|
|
|
|
console.log('in useEffect')
|
|
|
|
})
|
|
|
|
return <p>Hello world</p>
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Useful Links
|
2023-01-04 12:01:50 +01:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
[Server and Client Components](/docs/getting-started/react-essentials)
|