fix: move useMemo before early returns to prevent React error #310#1045
fix: move useMemo before early returns to prevent React error #310#1045
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
… 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>
There was a problem hiding this comment.
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)aboveisLoading/!objectDefearly returns to prevent React error #310. - Added an internal
!objectDefguard inside the memo callback to safely return a minimal schema. - Inlined intermediate schema-building variables into the
useMemocallback.
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [objectDef?.name, pureRecordId, childRelatedData, actionRefreshKey]); |
There was a problem hiding this comment.
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.
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, [objectDef?.name, pureRecordId, childRelatedData, actionRefreshKey]); | |
| }, [ | |
| objectDef, | |
| childRelations, | |
| t, | |
| pureRecordId, | |
| childRelatedData, | |
| actionRefreshKey, | |
| ]); |
RecordDetailViewcrashes with React error #310 ("Rendered more hooks than during the previous render") because theuseMemo(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.useMemobefore early returns with a!objectDefguard inside the callback returning a minimal unused schemaprimaryField,sections,highlightFields,sectionGroups,related,recordHeaderActions) into theuseMemocallback — they were only consumed bydetailSchemaanyway💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.