What is the best way to organize shared components in Next.js to avoid unnecessary re-renders? | Entelico QA
Knowledge Base

What is the best way to organize shared components in Next.js to avoid unnecessary re-renders?

Quick Answer: The best way to organize shared components in Next.js is to keep them presentational, stateless where possible, and colocated by feature rather than forcing everything into a global "components" folder. To avoid unnecessary re-renders, memoize truly stable components with `React.memo`, pass primitive props instead of new object/function references on every render, and isolate state so that only the smallest necessary subtree updates.

Detailed Explanation

In Next.js, the highest-performance component architecture is usually a feature-first structure with a clear separation between shared UI primitives, reusable layout elements, and page-specific compositions. This reduces coupling and prevents broad re-render cascades caused by importing stateful, over-generalized components into multiple routes. Performance is preserved by designing shared components to be pure and predictable, using `React.memo` only where props are stable, and avoiding unnecessary context overuse, inline callbacks, and recreated objects that trigger updates even when visible output does not change.

Key Technical Drivers

  • Organize shared components by responsibility: keep low-level UI primitives in a design-system layer, domain-specific components near their feature, and avoid a single catch-all components directory that encourages accidental coupling.
  • Minimize re-renders by ensuring shared components receive stable props: use `useCallback` and `useMemo` for derived values only when needed, and prefer passing IDs, strings, and booleans instead of freshly created objects or arrays.
  • Localize state and rendering boundaries: move volatile state as deep as possible, wrap pure shared components with `React.memo`, and use context sparingly so updates do not propagate through unrelated parts of the tree.