Performance: Managing large applications
This affects initial page load speed, bundle size, and runtime responsiveness in large React apps.
import React, { Suspense, lazy } from 'react'; const BigComponentA = lazy(() => import('./BigComponentA')); const BigComponentB = lazy(() => import('./BigComponentB')); export default function App() { return ( <> <Suspense fallback={<div>Loading A...</div>}> <BigComponentA /> </Suspense> <Suspense fallback={<div>Loading B...</div>}> <BigComponentB /> </Suspense> </> ); }
import { BigComponentA } from './BigComponentA'; import { BigComponentB } from './BigComponentB'; export default function App() { return ( <> <BigComponentA /> <BigComponentB /> </> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Import all components upfront | High (all nodes loaded) | Multiple reflows on load | High paint cost | [X] Bad |
| Lazy load components with React.lazy | Reduced initial nodes | Single reflow on load | Lower paint cost | [OK] Good |
| Prop drilling global state | Many re-renders | Many reflows on state change | High paint cost | [X] Bad |
| Use React Context for global state | Fewer re-renders | Fewer reflows | Lower paint cost | [OK] Good |
| Inline event handlers per item | Many function recreations | No direct reflow but high CPU | Higher paint cost due to CPU | [X] Bad |
| Memoized shared event handler | Single function instance | No extra reflows | Lower paint cost | [OK] Good |
Header from a file in React?import Header from './Header'; is correct for default exports.require is CommonJS, not standard in React apps; include is invalid; named import { Header } requires named export.import X from [OK]src/
components/
Navbar.js
pages/
Home.js
About.js
App.jsfunction App() {
const [count, setCount] = useState(0)
return (
<div>
<button onClick="setCount(count + 1)">Increment</button>
<p>Count: {count}</p>
</div>
)
}Pass the test and this pattern moves unknown