Skip to content

fix: move useMemo before early returns to prevent React error #310#1045

Merged
hotlong merged 2 commits intomainfrom
copilot/fix-minified-react-error-again
Mar 9, 2026
Merged

fix: move useMemo before early returns to prevent React error #310#1045
hotlong merged 2 commits intomainfrom
copilot/fix-minified-react-error-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 9, 2026

RecordDetailView crashes with React error #310 ("Rendered more hooks than during the previous render") because the useMemo(detailSchema) hook is placed after two conditional early returns (isLoading, !objectDef). First render calls 24 hooks and returns early; next render calls 25, triggering the error.

  • Move useMemo before early returns with a !objectDef guard inside the callback returning a minimal unused schema
  • Fold intermediate variables (primaryField, sections, highlightFields, sectionGroups, related, recordHeaderActions) into the useMemo callback — they were only consumed by detailSchema anyway
// Before: useMemo after early returns = inconsistent hook count
if (isLoading) return <SkeletonDetail />;
if (!objectDef) return <Empty />;
const primaryField = ...;
const sections = ...;
const detailSchema = useMemo(() => ({ ... }), [...]); // ← hook #25, skipped on early return

// After: all hooks called unconditionally
const detailSchema = useMemo(() => {
  if (!objectDef) return { type: 'detail-view' } as DetailViewSchema;
  // ... all computation moved inside
}, [objectDef?.name, pureRecordId, childRelatedData, actionRefreshKey]);

if (isLoading) return <SkeletonDetail />;
if (!objectDef) return <Empty />;

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 9, 2026

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

Project Deployment Actions Updated (UTC)
objectui Ready Ready Preview, Comment Mar 9, 2026 2:16am
objectui-demo Ready Ready Preview, Comment Mar 9, 2026 2:16am
objectui-storybook Ready Ready Preview, Comment Mar 9, 2026 2:16am

Request Review

… error #310

The useMemo hook for detailSchema was placed after conditional early
returns (isLoading / !objectDef), causing an inconsistent hook count
across renders. React error #310 ("Rendered more hooks than during the
previous render") was triggered when transitioning from an early-return
render to a full render.

Move the useMemo before the early returns with a guard clause for
!objectDef inside the callback. All intermediate computation
(primaryField, sections, highlightFields, etc.) is folded into the
useMemo since those variables are only consumed by detailSchema.

Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix minified React error #310 in the application fix: move useMemo before early returns to prevent React error #310 Mar 9, 2026
@hotlong hotlong marked this pull request as ready for review March 9, 2026 02:20
Copilot AI review requested due to automatic review settings March 9, 2026 02:20
@hotlong hotlong merged commit 6b6cdfb into main Mar 9, 2026
6 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a React hook-order crash in RecordDetailView by ensuring useMemo(detailSchema) is always called before any conditional early returns, keeping the hook count consistent across renders.

Changes:

  • Moved useMemo(detailSchema) above isLoading / !objectDef early returns to prevent React error #310.
  • Added an internal !objectDef guard inside the memo callback to safely return a minimal schema.
  • Inlined intermediate schema-building variables into the useMemo callback.

Comment on lines +422 to +423
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [objectDef?.name, pureRecordId, childRelatedData, actionRefreshKey]);
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

detailSchema's useMemo callback closes over objectDef (label/views/actions/fields), childRelations, and t(...), but the dependency array only includes objectDef?.name, pureRecordId, childRelatedData, and actionRefreshKey (and disables exhaustive-deps). This can leave detailSchema stale if the object definition updates without a name change, if reverse relations change, or if translations change. Prefer adding the actual referenced values to the deps (e.g., objectDef, childRelations, t) and removing the eslint disable so schema stays in sync.

Suggested change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [objectDef?.name, pureRecordId, childRelatedData, actionRefreshKey]);
}, [
objectDef,
childRelations,
t,
pureRecordId,
childRelatedData,
actionRefreshKey,
]);

Copilot uses AI. Check for mistakes.
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.

3 participants