Performance: React Router overview
React Router affects page load speed and interaction responsiveness by managing client-side navigation without full page reloads.
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom'; import About from './About'; import Contact from './Contact'; function App() { return ( <BrowserRouter> <nav> <Link to="/about">About</Link> <Link to="/contact">Contact</Link> </nav> <Routes> <Route path="/about" element={<About />} /> <Route path="/contact" element={<Contact />} /> </Routes> </BrowserRouter> ); } export default App;
import React from 'react'; function App() { return ( <div> <a href="/about">About</a> <a href="/contact">Contact</a> </div> ); } export default App;
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Anchor tags for navigation | Full DOM reload | Multiple reflows (full page) | High paint cost (full repaint) | [X] Bad |
| React Router with Link and Routes | Minimal DOM updates | Single reflow per route change | Low paint cost (partial repaint) | [OK] Good |
React Router in a React application?BrowserRouter to wrap the app.Route defines pages, Link creates navigation links, and Router alone is not a valid component./about?
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element=<Home /> />
<Route path='/about' element=<About /> />
</Routes>
</BrowserRouter>
);
}/about matches the route with path='/about', so <About /> renders.<Home /> renders only at /.import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Route path='/' element=<Home /> />
<Routes>
<Route path='/about' element=<About /> />
</Routes>
</BrowserRouter>
);
}<Route> components must be children of <Routes>.<Route path='/' ... /> is outside <Routes>, which is invalid.<Link> is designed to change the URL and render new pages without reloading.<a> causes full page reload, <Route> defines pages, and <BrowserRouter> wraps the app but does not create links.Pass the test and this pattern moves unknown