Skip to content

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

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

fix(react): Prevent custom page remounts in profile components#8604
jescalan wants to merge 6 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 Jun 2, 2026 9:33pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 20, 2026

🦋 Changeset detected

Latest commit: 93d045e

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

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 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 | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly and specifically describes the main fix: preventing custom page remounts in profile components, which is the primary objective of the changeset.
Description check ✅ Passed The PR description is directly related to the changeset, explaining the fix for custom page remounts, portal stability, and length change detection improvements.
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/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: 93d045e

@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
@jescalan
Copy link
Copy Markdown
Contributor Author

@codex review

@jescalan
Copy link
Copy Markdown
Contributor Author

@wobsoriano @anagstef any chance you might be able to take a look here? 🙏

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

ℹ️ 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".

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

Break Check: no API changes detected across the tracked packages.

Copy link
Copy Markdown
Member

@jacekradko jacekradko left a comment

Choose a reason for hiding this comment

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

Verified this addresses the remounts

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)
integration/templates/react-vite/src/custom-user-profile/index.tsx (1)

15-27: ⚡ Quick win

Add explicit type="button" to the new controls.

These buttons currently rely on the implicit default of submit, which makes the template fragile if it ever ends up inside a form.

♻️ Suggested fix
       <button
+        type='button'
         data-page={1}
         onClick={() => setCounter(a => a + 1)}
       >
         Update
       </button>
@@
       <button
+        type='button'
         data-local-counter={1}
         onClick={() => setLocalCounter(a => a + 1)}
       >
         Increment local
       </button>
@@
       <button
+        type='button'
         data-testid='rerender-parent'
         onClick={() => setParentTick(t => t + 1)}
       >
         Rerender parent: {parentTick}
       </button>

As per coding guidelines, Use proper button types in React components.

Also applies to: 40-45

🤖 Prompt for 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.

In `@integration/templates/react-vite/src/custom-user-profile/index.tsx` around
lines 15 - 27, The two new button elements that call setCounter and
setLocalCounter (and the similar buttons around the setLocalCounter at lines
~40-45) currently rely on the default submit behavior; update each <button> used
as a control (the ones invoking setCounter and setLocalCounter in this
component) to include an explicit type="button" attribute so they do not submit
enclosing forms unexpectedly.
🤖 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.

Nitpick comments:
In `@integration/templates/react-vite/src/custom-user-profile/index.tsx`:
- Around line 15-27: The two new button elements that call setCounter and
setLocalCounter (and the similar buttons around the setLocalCounter at lines
~40-45) currently rely on the default submit behavior; update each <button> used
as a control (the ones invoking setCounter and setLocalCounter in this
component) to include an explicit type="button" attribute so they do not submit
enclosing forms unexpectedly.

ℹ️ Review info
⚙️ Run configuration

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

Review profile: CHILL

Plan: Pro

Run ID: 5f6d5efb-b086-466a-afa0-d3bc85011d45

📥 Commits

Reviewing files that changed from the base of the PR and between f82b0ab and 93d045e.

📒 Files selected for processing (9)
  • .changeset/custom-page-portal-stable-keys.md
  • integration/templates/react-vite/src/custom-user-profile/index.tsx
  • integration/tests/custom-pages.test.ts
  • packages/react/src/components/uiComponents.tsx
  • packages/react/src/utils/__tests__/customPages.remount.integration.test.tsx
  • packages/react/src/utils/__tests__/useCustomMenuItems.test.tsx
  • packages/react/src/utils/__tests__/useCustomPages.test.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/components/uiComponents.tsx
  • .changeset/custom-page-portal-stable-keys.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/react/src/utils/tests/useCustomMenuItems.test.tsx
  • packages/react/src/utils/tests/useCustomPages.test.tsx
  • packages/react/src/utils/useCustomPages.tsx
  • packages/react/src/utils/useCustomMenuItems.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants