Skip to content

Commit f581a73

Browse files
committed
fix: hydration ssr
1 parent 3ab306f commit f581a73

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/UnistylesTheme.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, { createContext, useEffect, useState } from 'react'
1+
import React, { createContext } from 'react'
22
import type { PropsWithChildren } from 'react'
3-
import { isServer } from './utils'
43

54
interface UnistylesThemeProps extends PropsWithChildren {
65
theme: any
@@ -11,16 +10,8 @@ export const UnistylesContext = createContext({})
1110
export const UnistylesTheme: React.FunctionComponent<UnistylesThemeProps> = ({
1211
theme,
1312
children
14-
}) => {
15-
const [isClient, setIsClient] = useState(!isServer)
16-
17-
useEffect(() => {
18-
setIsClient(true)
19-
}, [])
20-
21-
return (
22-
<UnistylesContext.Provider value={theme}>
23-
{isClient ? children : <React.Fragment /> }
24-
</UnistylesContext.Provider>
25-
)
26-
}
13+
}) => (
14+
<UnistylesContext.Provider value={theme}>
15+
{children}
16+
</UnistylesContext.Provider>
17+
)

src/hooks/useDimensions.web.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
22
import type { ScreenSize } from '../types'
33

44
export const useDimensions = (): ScreenSize => {
55
const timerRef = useRef<ReturnType<typeof setTimeout>>()
6-
const [screenSize, setScreenSize] = useState<ScreenSize>({
7-
width: window.innerWidth,
8-
height: window.innerHeight
9-
})
6+
const [screenSize, setScreenSize] = useState<ScreenSize>({} as ScreenSize)
7+
8+
useLayoutEffect(() => {
9+
setScreenSize({
10+
width: window.innerWidth,
11+
height: window.innerHeight
12+
})
13+
}, [])
1014

1115
useEffect(() => {
1216
const handleResize = () => {

0 commit comments

Comments
 (0)