Skip to content

Commit

Permalink
Add dynamic loading as a way to avoid hydration mismatch in README (#249
Browse files Browse the repository at this point in the history
)

Update README.md

Co-authored-by: Paco <34928425+pacocoursey@users.noreply.github.com>
  • Loading branch information
Judge-Paul and pacocoursey authored Nov 4, 2024
1 parent b3baf27 commit 23c00a8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions next-themes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,24 @@ const ThemeSwitch = () => {
export default ThemeSwitch
```

Alternatively, you could lazy load the component on the client side. The following example uses `next/dynamic` but you could also use `React.lazy`:

```js
import dynamic from 'next/dynamic'

const ThemeSwitch = dynamic(() => import('./ThemeSwitch'), { ssr: false })

const ThemePage = () => {
return (
<div>
<ThemeSwitch />
</div>
)
}

export default ThemePage
```

To avoid [Layout Shift](https://web.dev/cls/), consider rendering a skeleton/placeholder until mounted on the client side.

#### Images
Expand Down

0 comments on commit 23c00a8

Please sign in to comment.