Skip to content

Commit 3a3c7ab

Browse files
committed
Delay rendering to avoid SSR hydration errors
Closes #2
1 parent 65c2aee commit 3a3c7ab

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

packages/react-hook-tracer/src/components/StrictModeWarning.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import { useEffect, useState } from 'react'
2+
13
import * as hookUtil from '../hooks/hookUtil'
24

35
import './StrictModeWarning.css'
46

57
export const StrictModeWarning = (): JSX.Element => {
8+
const [isInitialized, setIsInitialized] = useState(false)
9+
10+
useEffect(() => {
11+
setIsInitialized(true)
12+
}, [])
13+
614
const strictMode = hookUtil.useStrictModeDetector()
715

16+
// Setting data-strict-mode is delayed until after initialization effect to avoid SSR hydration mismatch.
817
return (
9-
<div className="strict-mode-warning" data-strict-mode={strictMode}>
18+
<div className="strict-mode-warning" data-strict-mode={isInitialized && strictMode}>
1019
Warning: React.StictMode is active, trace log will not be accurate. (
1120
<a
1221
href="https://github.com/Oblosys/react-hook-tracer#react-strict-mode"

packages/react-hook-tracer/src/components/TracePanel.tsx

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export const TracePanel = ({
2828
traceOrigins,
2929
refreshTracePanelRef,
3030
}: TracePanelProps) => {
31+
const [isInitialized, setIsInitialized] = useState(false)
32+
33+
useEffect(() => {
34+
setIsInitialized(true)
35+
}, [])
36+
3137
const [selectedLogEntry, setSelectedLogEntry] = useState<LogEntry | null>(null)
3238

3339
const [, setRefreshDummy] = useState([]) // Dummy state to trigger re-render.
@@ -58,32 +64,37 @@ export const TracePanel = ({
5864
const { mount, render, trace, hooks } = traceOrigins
5965
const traceOriginList: TraceOrigin[] = [mount, render, ...hooks, trace]
6066

67+
// Rendering inner trace panel is delayed until after initialization effect to avoid SSR hydration mismatch.
6168
return (
6269
<TracePanelWrapper>
63-
<div className="component-label">
64-
<div>{componentLabel}</div>
65-
</div>
66-
{propKeyValues.length > 0 && (
67-
<div className="props">
68-
{propKeyValues.map(({ propKey, propValue }) => (
69-
<Prop
70-
propKey={propKey}
71-
propValue={propValue}
72-
showPropValue={showPropValue}
73-
key={propKey}
74-
/>
75-
))}
76-
</div>
70+
{isInitialized && (
71+
<>
72+
<div className="component-label">
73+
<div>{componentLabel}</div>
74+
</div>
75+
{propKeyValues.length > 0 && (
76+
<div className="props">
77+
{propKeyValues.map(({ propKey, propValue }) => (
78+
<Prop
79+
propKey={propKey}
80+
propValue={propValue}
81+
showPropValue={showPropValue}
82+
key={propKey}
83+
/>
84+
))}
85+
</div>
86+
)}
87+
<div className="trace-origins">
88+
{traceOriginList.map((origin, index) => (
89+
<TraceOriginEntry
90+
key={index}
91+
traceOrigin={origin}
92+
isHighlighted={origin === selectedLogEntry?.origin}
93+
/>
94+
))}
95+
</div>
96+
</>
7797
)}
78-
<div className="trace-origins">
79-
{traceOriginList.map((origin, index) => (
80-
<TraceOriginEntry
81-
key={index}
82-
traceOrigin={origin}
83-
isHighlighted={origin === selectedLogEntry?.origin}
84-
/>
85-
))}
86-
</div>
8798
</TracePanelWrapper>
8899
)
89100
}

0 commit comments

Comments
 (0)