Skip to content

fix(clarity): dual React instance crash in SSR — sonner resolves wrong react #124

Description

@janprasil

Problem

Running tools clarity ui crashes during SSR with:

TypeError: null is not an object (evaluating 'resolveDispatcher().useState')
    at Toaster (node_modules/sonner/dist/index.mjs:1065:21)

Root Cause

Dual React instance during server-side rendering:

  • sonner was only declared in the root package.json → installed in root node_modules/ → imports root react (v19.2.0)
  • The clarity UI app has its own node_modules/react (v19.2.4) and react-dom (v19.2.4)
  • During SSR, react-dom-server sets up its internal dispatcher on the local react instance
  • When sonner's <Toaster> calls useState, it goes through the root react instance which has no dispatcher → resolveDispatcher() returns null → crash

Fix

Add sonner to src/clarity/ui/package.json dependencies so it resolves through the local node_modules/ and uses the same react instance as the rest of the app.

# src/clarity/ui/package.json
  "lucide-react": "^0.544.0",
  "react": "^19.2.1",
+ "sonner": "^2.0.7",
  "react-dom": "^19.2.1",

Then bun install in src/clarity/ui/.

Verification

After the fix, both the app and sonner resolve to the same react:

sonner → src/clarity/ui/node_modules/react/index.js
app    → src/clarity/ui/node_modules/react/index.js

Note

Any package that uses React hooks and is only in the root node_modules/ (not the dashboard's local node_modules/) will hit the same issue during SSR. Dashboard apps with their own node_modules/react must declare all React-dependent packages locally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions