Skip to content

Commit b7e26a5

Browse files
add: dependency hook added v2
1 parent 2f1c913 commit b7e26a5

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

custom-implementation/src/components/theme-switch.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@ import React, { useEffect } from 'react'
22

33
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
44

5-
const useReactPath = () => {
5+
export const ThemeSwitch = () => {
66
const [path, setPath] = React.useState(window.location.pathname)
7-
const listenToPopstate = () => {
8-
const winPath = window.location.pathname
9-
setPath(winPath)
10-
}
11-
React.useEffect(() => {
7+
const [selected, setSelected] = React.useState('public')
8+
9+
useEffect(() => {
10+
const listenToPopstate = () => {
11+
const winPath = window.location.pathname
12+
setPath(winPath)
13+
}
1214
window.addEventListener('popstate', listenToPopstate)
1315
return () => {
1416
window.removeEventListener('popstate', listenToPopstate)
1517
}
1618
}, [])
17-
return path
18-
}
19-
20-
export const ThemeSwitch = () => {
21-
const [selected, setSelected] = React.useState('public')
22-
const pathname = useReactPath()
2319

2420
useEffect(() => {
25-
const beta = pathname.split('/').includes('beta')
21+
const beta = path.split('/').includes('beta')
2622
setSelected(beta ? 'beta' : 'public')
27-
}, [pathname])
23+
}, [path])
2824

2925
return (
3026
<div className="mt-2">

0 commit comments

Comments
 (0)