Skip to content

Commit cb47fcc

Browse files
authored
Merge pull request #1 from ianalloway/devin/1771201638-improve-docs
docs: Add troubleshooting section to 'use client' reference
2 parents 55a317d + 24d9b45 commit cb47fcc

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

src/content/reference/rsc/use-client.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,96 @@ These libraries may rely on component Hooks or client APIs. Third-party componen
376376

377377
If these libraries have been updated to be compatible with React Server Components, then they will already include `'use client'` markers of their own, allowing you to use them directly from your Server Components. If a library hasn't been updated, or if a component needs props like event handlers that can only be specified on the client, you may need to add your own Client Component file in between the third-party Client Component and your Server Component where you'd like to use it.
378378

379-
[TODO]: <> (Troubleshooting - need use-cases)
379+
## Troubleshooting {/*troubleshooting*/}
380+
381+
### "Functions cannot be passed directly to Client Components" error {/*functions-cannot-be-passed-directly*/}
382+
383+
This error occurs when you try to pass a function from a Server Component to a Client Component.
384+
385+
```js
386+
// Server Component
387+
import ClientButton from './ClientButton';
388+
389+
export default function Page() {
390+
function handleClick() {
391+
console.log('clicked');
392+
}
393+
// This will error!
394+
return <ClientButton onClick={handleClick} />;
395+
}
396+
```
397+
398+
Functions are not serializable, so they cannot be passed from server to client. To fix this, either move the function to the Client Component, or use a [Server Function](/reference/rsc/server-functions) with `'use server'`.
399+
400+
```js
401+
// Solution 1: Move the handler to the Client Component
402+
'use client';
403+
404+
export default function ClientButton() {
405+
function handleClick() {
406+
console.log('clicked');
407+
}
408+
return <button onClick={handleClick}>Click me</button>;
409+
}
410+
```
411+
412+
```js
413+
// Solution 2: Use a Server Function
414+
'use server';
415+
416+
export async function handleClick() {
417+
console.log('clicked on server');
418+
}
419+
```
420+
421+
### Component uses hooks but doesn't have `'use client'` {/*component-uses-hooks-without-use-client*/}
422+
423+
If you're using hooks like `useState`, `useEffect`, or `useContext` in a component, you need to mark it as a Client Component.
424+
425+
```js
426+
// This will error - hooks require 'use client'
427+
import { useState } from 'react';
428+
429+
export default function Counter() {
430+
const [count, setCount] = useState(0);
431+
return <button onClick={() => setCount(count + 1)}>{count}</button>;
432+
}
433+
```
434+
435+
Add `'use client'` at the top of the file:
436+
437+
```js
438+
'use client';
439+
440+
import { useState } from 'react';
441+
442+
export default function Counter() {
443+
const [count, setCount] = useState(0);
444+
return <button onClick={() => setCount(count + 1)}>{count}</button>;
445+
}
446+
```
447+
448+
### Third-party library doesn't work in Server Components {/*third-party-library-server-component*/}
449+
450+
Many third-party libraries were written before Server Components existed and may use client-only features. If you get an error when importing a library in a Server Component, create a wrapper Client Component:
451+
452+
```js
453+
// components/ChartWrapper.js
454+
'use client';
455+
456+
import { Chart } from 'third-party-chart-library';
457+
458+
export default function ChartWrapper(props) {
459+
return <Chart {...props} />;
460+
}
461+
```
462+
463+
Then import the wrapper in your Server Component:
464+
465+
```js
466+
// page.js (Server Component)
467+
import ChartWrapper from './components/ChartWrapper';
468+
469+
export default function Page() {
470+
return <ChartWrapper data={data} />;
471+
}

0 commit comments

Comments
 (0)