SmartCodingTips

🚀 React Performance Tips

React is fast by default, but poor practices or large-scale apps can introduce lag. Here's a collection of practical tips to boost performance and maintain a responsive UI.


✅ 1. General Tips

  • Keep components small and focused
  • Use key props correctly in lists
  • Avoid unnecessary re-renders with React.memo
  • Split large pages using code-splitting/lazy loading

🔁 2. useMemo & useCallback

  • Use useMemo to memoize expensive calculations
  • Use useCallback to prevent re-creating functions on each render
  • Avoid overusing them — they add overhead if misused

📦 3. Virtualize Long Lists

Use libraries like react-window or react-virtualized to render only visible items instead of the full list.

🧠 4. Avoid Inline Functions in JSX

Inline arrow functions create new instances on every render, possibly causing unnecessary re-renders in child components.

📌 5. Avoid New Objects/Arrays in Props

Doing <Comp style={{ margin: 10 }} /> or <Comp items={[]} /> creates new references, triggering re-renders.

🛠️ 6. Use DevTools & Profiler

  • Use React DevTools for inspecting component re-renders
  • Use <Profiler> to measure actual render time
  • Use why-did-you-render in dev to detect unwanted renders

📦 7. Optimize Bundle Size

  • Use React.lazy and Suspense for component-level splitting
  • Analyze and reduce bundle size using tools like webpack-bundle-analyzer
  • Load images and assets lazily

🧩 8. Keep State Local Where Possible

  • Move state close to the component that uses it
  • Global state updates can trigger unnecessary re-renders

📋 Summary

  • Measure performance before optimizing
  • Virtualize lists and defer heavy content
  • Memoize only when necessary
  • Split and lazy-load code as needed