Skip to content

fix(examples): guard the react shadow-dom mount target#10431

Merged
TkDodo merged 1 commit intoTanStack:mainfrom
grzdev:fix-react-shadow-dom-mount-target
Apr 9, 2026
Merged

fix(examples): guard the react shadow-dom mount target#10431
TkDodo merged 1 commit intoTanStack:mainfrom
grzdev:fix-react-shadow-dom-mount-target

Conversation

@grzdev
Copy link
Copy Markdown
Contributor

@grzdev grzdev commented Apr 8, 2026

🎯 Changes

Adds an explicit mount target guard to the React shadow-dom example.

Previously, this entrypoint wrapped rendering in if (appRoot), which meant a
missing #root element caused the example to fail silently with a blank page.

This change throws a clear Missing #root element error instead, matching the
pattern used in other example safety fixes. It also removes the
appRoot.shadowRoot! non-null assertion by passing the created shadowRoot
directly to the devtools.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Added validation to ensure the root element exists before initialization, throwing an error if missing.
  • Refactor

    • Simplified initialization logic by removing conditional wrappers and making the setup flow more straightforward.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

The example file adds a runtime guard to fail immediately when the #root element is missing, removes conditional initialization logic, and updates ReactQueryDevtools to reference a newly created Shadow DOM instance instead of the unsafe non-null assertion.

Changes

Cohort / File(s) Summary
React Shadow DOM Example
examples/react/shadow-dom/src/main.tsx
Added explicit error handling for missing #root element, removed conditional wrapper around app initialization, created and properly referenced Shadow DOM instance, and updated ReactQueryDevtools shadowDOMTarget prop reference.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • TanStack/query#10226: Adds the same runtime guard for missing "#root" mount element in React example entry files, demonstrating a shared pattern across related examples.

Poem

🐰 A shadow emerges from the root so deep,
Where errors now guard what we seek to keep,
DevTools now see without assertion's sleight,
The example springs forth, robust and right! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a guard to the react shadow-dom mount target, which aligns with the actual code modification that adds error handling for missing #root element.
Description check ✅ Passed The description follows the template structure with complete 🎯 Changes section, ✅ Checklist with items checked, and 🚀 Release Impact marked as docs/CI/dev-only, covering all required sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 8, 2026

View your CI Pipeline Execution ↗ for commit 6128696

Command Status Duration Result
nx run-many --target=build --exclude=examples/*... ✅ Succeeded <1s View ↗
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 19s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-08 22:04:48 UTC

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 8, 2026

More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10431

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10431

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@10431

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10431

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10431

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10431

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10431

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@10431

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@10431

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10431

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10431

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@10431

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@10431

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10431

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10431

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@10431

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10431

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10431

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@10431

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10431

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10431

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@10431

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10431

commit: 6128696

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
examples/react/shadow-dom/src/main.tsx (1)

12-13: Reuse existing open shadow root to avoid HMR re-entry errors.

The suggested pattern safely avoids calling attachShadow() on a previously-created open shadow root, which would throw during module reload in dev workflows.

♻️ Suggested tweak
-const shadowRoot = appRoot.attachShadow({ mode: 'open' })
+const shadowRoot = appRoot.shadowRoot ?? appRoot.attachShadow({ mode: 'open' })

Note: This pattern is safe because the code uses { mode: 'open' }; it would not work if the mode were 'closed'.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/react/shadow-dom/src/main.tsx` around lines 12 - 13, The current
code always calls appRoot.attachShadow({ mode: 'open' }) which throws on HMR
re-entry; change it to reuse an existing open shadow root if present by checking
appRoot.shadowRoot and using that when available, otherwise call
appRoot.attachShadow({ mode: 'open' }), then pass the resulting shadowRoot into
ReactDOM.createRoot; update the code around the shadowRoot variable and the
ReactDOM.createRoot(...) call (referencing appRoot, shadowRoot, and
ReactDOM.createRoot) to implement this conditional reuse.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@examples/react/shadow-dom/src/main.tsx`:
- Around line 12-13: The current code always calls appRoot.attachShadow({ mode:
'open' }) which throws on HMR re-entry; change it to reuse an existing open
shadow root if present by checking appRoot.shadowRoot and using that when
available, otherwise call appRoot.attachShadow({ mode: 'open' }), then pass the
resulting shadowRoot into ReactDOM.createRoot; update the code around the
shadowRoot variable and the ReactDOM.createRoot(...) call (referencing appRoot,
shadowRoot, and ReactDOM.createRoot) to implement this conditional reuse.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8694c110-826b-4406-bad0-748f44569c4a

📥 Commits

Reviewing files that changed from the base of the PR and between ef62f7b and 6128696.

📒 Files selected for processing (1)
  • examples/react/shadow-dom/src/main.tsx

@TkDodo TkDodo merged commit 05771b1 into TanStack:main Apr 9, 2026
10 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.

2 participants