Skip to content

fix(react): Prevent custom page remounts in profile components#8604

Open
jescalan wants to merge 4 commits into
mainfrom
je/orgprofile-custom-pages-rendering-bug
Open

fix(react): Prevent custom page remounts in profile components#8604
jescalan wants to merge 4 commits into
mainfrom
je/orgprofile-custom-pages-rendering-bug

Conversation

@jescalan
Copy link
Copy Markdown
Contributor

@jescalan jescalan commented May 20, 2026

Summary

Prevents custom pages rendered through React profile components from remounting on parent rerenders. The portal wrapper now keeps stable portal component identities while still rendering the latest React node into the Clerk-owned mount node, so customer custom page trees do not unmount/remount during expected Clerk rerenders such as focus-driven data refreshes. Also fixes custom page/menu length change detection in the host renderer.

Changes in this repo

Stabilizes custom element portal identities in @clerk/react, fixes customPages/customMenuItems length change detection in ClerkHostRenderer, and adds focused regressions for rerender-without-remount, falsy ReactNode portal content, and custom page length updates.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview, Comment May 22, 2026 8:55pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 20, 2026

🦋 Changeset detected

Latest commit: f82b0ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@clerk/react Patch
@clerk/chrome-extension Patch
@clerk/expo Patch
@clerk/nextjs Patch
@clerk/react-router Patch
@clerk/tanstack-react-start Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jescalan
Copy link
Copy Markdown
Contributor Author

@codex review

@jescalan jescalan marked this pull request as ready for review May 20, 2026 17:47
@jescalan jescalan changed the title Prevent custom page remounts in profile components fix(react): Prevent custom page remounts in profile components May 20, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 76a0f88d-5e9d-4412-b90f-75546380f867

📥 Commits

Reviewing files that changed from the base of the PR and between 6bde33b and f82b0ab.

📒 Files selected for processing (9)
  • .changeset/fix-org-profile-custom-page-remounts.md
  • packages/react/src/components/ClerkHostRenderer.tsx
  • packages/react/src/components/__tests__/ClerkHostRenderer.test.tsx
  • packages/react/src/utils/__tests__/useCustomElementPortal.test.tsx
  • packages/react/src/utils/__tests__/useCustomMenuItems.test.tsx
  • packages/react/src/utils/__tests__/useCustomPages.test.tsx
  • packages/react/src/utils/useCustomElementPortal.tsx
  • packages/react/src/utils/useCustomMenuItems.tsx
  • packages/react/src/utils/useCustomPages.tsx
✅ Files skipped from review due to trivial changes (1)
  • .changeset/fix-org-profile-custom-page-remounts.md

📝 Walkthrough

Walkthrough

This PR refactors useCustomElementPortal to use ref-backed portal caching and widens portal id types and portal API to a React.ComponentType. It adds stable portalId generation for custom menu items and custom pages, updates ClerkHostRenderer to compare previous/current customPages/customMenuItems lengths with null-safe props handling, and introduces tests that verify portal persistence, identity across insertions, and rendering of falsy content.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: preventing custom page remounts in profile components, which is the core objective of this pull request.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem being solved and the solution approach with specific technical details about portal stability and mount node handling.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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


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

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 592c4eee6b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/react/src/utils/useCustomElementPortal.tsx
@jescalan
Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

✅ Actions performed

Full review triggered.

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/react/src/utils/useCustomMenuItems.tsx`:
- Around line 199-210: getCustomMenuItemPortalId is currently generating
non-keyed fallback IDs from label+target which can collide; change its contract
so fallback IDs are collision-safe: either require the caller to pass a stable
fallback identifier (e.g., an index or generated uid) and include that in the
returned string, or make getCustomMenuItemPortalId append a stable per-item uid
(not just label/target) so two items with identical label/target get distinct
IDs; if you opt to require explicit keys, make the function throw/console.error
when no stable id/key is supplied so callers must fix duplication. Update all
call sites that invoke getCustomMenuItemPortalId (where lookup happens) to pass
the new stable identifier or handle the thrown error so portal lookups (the
places using the id) never alias different menu items.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: e9065694-5486-4e33-9310-c73644931340

📥 Commits

Reviewing files that changed from the base of the PR and between 592c4ee and c85645f.

📒 Files selected for processing (5)
  • packages/react/src/utils/__tests__/useCustomMenuItems.test.tsx
  • packages/react/src/utils/__tests__/useCustomPages.test.tsx
  • packages/react/src/utils/useCustomElementPortal.tsx
  • packages/react/src/utils/useCustomMenuItems.tsx
  • packages/react/src/utils/useCustomPages.tsx

Comment thread packages/react/src/utils/useCustomMenuItems.tsx
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 20, 2026

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@8604

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@8604

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@8604

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@8604

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@8604

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@8604

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@8604

@clerk/express

npm i https://pkg.pr.new/@clerk/express@8604

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@8604

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@8604

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@8604

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@8604

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@8604

@clerk/react

npm i https://pkg.pr.new/@clerk/react@8604

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@8604

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@8604

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@8604

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@8604

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@8604

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@8604

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@8604

commit: f82b0ab

@jescalan jescalan force-pushed the je/orgprofile-custom-pages-rendering-bug branch from c85645f to 6bde33b Compare May 21, 2026 18:28
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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/react/src/utils/useCustomElementPortal.tsx`:
- Around line 20-23: The hook useCustomElementPortal currently coerces all
portal IDs to strings (String(id)) causing numeric and string IDs to collide;
update the internal maps (nodeMap, nodeMapRef, elementsRef, portalsRef) to key
by the original id type (string | number) instead of stringified keys and change
any places that call String(id) (around the blocks indicated at lines 25-36 and
37-61) to use the raw id value; also update the Map generic types from
Map<string, ...> to Map<string | number, ...> and ensure all lookups, sets and
refs consistently use the id as the key so numeric and string IDs remain
distinct.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: acb48737-0ce2-4089-953a-9fc73361a24c

📥 Commits

Reviewing files that changed from the base of the PR and between c85645f and 6bde33b.

📒 Files selected for processing (9)
  • .changeset/fix-org-profile-custom-page-remounts.md
  • packages/react/src/components/ClerkHostRenderer.tsx
  • packages/react/src/components/__tests__/ClerkHostRenderer.test.tsx
  • packages/react/src/utils/__tests__/useCustomElementPortal.test.tsx
  • packages/react/src/utils/__tests__/useCustomMenuItems.test.tsx
  • packages/react/src/utils/__tests__/useCustomPages.test.tsx
  • packages/react/src/utils/useCustomElementPortal.tsx
  • packages/react/src/utils/useCustomMenuItems.tsx
  • packages/react/src/utils/useCustomPages.tsx
✅ Files skipped from review due to trivial changes (2)
  • packages/react/src/utils/tests/useCustomMenuItems.test.tsx
  • .changeset/fix-org-profile-custom-page-remounts.md

Comment thread packages/react/src/utils/useCustomElementPortal.tsx Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6bde33bc81

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/react/src/components/ClerkHostRenderer.tsx Outdated
@jescalan jescalan force-pushed the je/orgprofile-custom-pages-rendering-bug branch from 6bde33b to f82b0ab Compare May 22, 2026 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant