Skip to content

fix(performance): dedupe React dependencies and cache lazy loaded category components - #1028

Merged
DavidHDev merged 1 commit into
DavidHDev:mainfrom
DarkFalc0n:fix/prevent-component-remounting
Aug 6, 2026
Merged

fix(performance): dedupe React dependencies and cache lazy loaded category components#1028
DavidHDev merged 1 commit into
DavidHDev:mainfrom
DarkFalc0n:fix/prevent-component-remounting

Conversation

@DarkFalc0n

@DarkFalc0n DarkFalc0n commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

The Bug

When Vite resolves multiple or duplicate instances of react and react-dom during module resolution or dynamic imports, React throws an Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Fixes #1029

PR Summary

This PR addresses duplicate React dependency issues and fixes unwanted component remounting during route renders on CategoryPage.

Changes Included

  • Vite Configuration (vite.config.js):
    • Added dedupe: ['react', 'react-dom'] under resolve to ensure single instance resolution of React across all imports/dependencies.
  • Category Page (src/pages/CategoryPage.jsx):
    • Introduced module-level lazyComponentCache (Map) to cache React.lazy() component factories.
    • Replaced inline lazy() instantiation during render with cached lookup to maintain stable component references and avoid unnecessary unmounting/remounting on re-renders.

Verification

  • Tested navigating across category and subcategory routes to confirm component state and animations remain intact during re-renders.
  • Verified local development build with npm run dev to ensure no "Invalid hook call" warnings or duplicate React runtime errors occur.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes local-dev “Invalid hook call” errors caused by duplicate React runtimes in Vite resolution, and prevents unnecessary unmount/remount behavior on CategoryPage by stabilizing React.lazy() component identities across renders.

Changes:

  • Add Vite resolve.dedupe for react and react-dom to ensure a single React instance is used.
  • Cache React.lazy() results at module scope in CategoryPage so the rendered subcategory component reference remains stable across re-renders.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
vite.config.js Forces Vite to dedupe react and react-dom to avoid duplicate runtime instances and hook call errors.
src/pages/CategoryPage.jsx Adds a module-level lazy-component cache to prevent remounts caused by re-creating React.lazy() on each render.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@DavidHDev
DavidHDev merged commit 3ba27d8 into DavidHDev:main Aug 6, 2026
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.

[BUG]: Invalid Hook Call Error in dev Vite app

3 participants