Skip to content

fix: prevent React 418 hydration mismatch on compare tools and client-only components - #670

Merged
tiberiuichim merged 1 commit into
developfrom
fix-compare-tools-ssr-hydration
Sep 4, 2026
Merged

fix: prevent React 418 hydration mismatch on compare tools and client-only components#670
tiberiuichim merged 1 commit into
developfrom
fix-compare-tools-ssr-hydration

Conversation

@tiberiuichim

Copy link
Copy Markdown
Member

Description

Fixes React error #418 (hydration mismatch) when navigating or opening tool pages while tools are saved in comparison storage (localStorage).

Changes

  1. Hydration safety for useCompareTools and CompareToolsPanel:
    • Added useHasMounted hook to safely defer client-only state derived from browser localStorage until after the component has mounted.
    • During SSR and the initial client hydration pass, selected tools evaluate to empty ([]), ensuring isSelected and isLimitReached match the server-rendered HTML.
    • Guarded <CompareToolsPanel /> with if (!hasMounted || effectiveSelectedTools.length === 0) return null; so that the floating panel is not rendered during initial hydration when SSR returned null.
  2. Hydration safety for withClientOnly HOC:
    • Replaced __SERVER__ ? null : <WrappedComponent /> check with a post-mount lifecycle guard (useState(false) + useEffect), preventing server/client tree mismatches during initial client hydration.
  3. Tests:
    • Added unit tests for useHasMounted and hasMounted in utils.test.jsx.
    • Added unit test for withClientOnly in withClientOnly.test.jsx.

@tiberiuichim
tiberiuichim merged commit 9fed699 into develop Sep 4, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant