Q1. What is React Router?
React Router is a standard library for routing in React applications.
It enables navigation between different components (views) without refreshing the page, keeping the UI in sync with the URL.
It provides components like
It enables navigation between different components (views) without refreshing the page, keeping the UI in sync with the URL.
It provides components like
BrowserRouter, Route, Link, etc.Q2. How does React Router work?
React Router uses a router component (like
When the URL matches a
Links (
BrowserRouter) that listens to URL changes.When the URL matches a
Route's path, that route renders its component.Links (
Link or NavLink) update the URL without a full page reload.Q3. What are the main components of React Router?
BrowserRouter: wraps the app and enables routing.
Routes: container for Route components.
Route: defines a mapping between a path and a component.
Link: creates navigation links.
NavLink: a Link with styling for active state.
Routes: container for Route components.
Route: defines a mapping between a path and a component.
Link: creates navigation links.
NavLink: a Link with styling for active state.
Q4. What is the difference between BrowserRouter and HashRouter?
BrowserRouter uses the HTML5 history API (clean URLs like
HashRouter uses the hash portion of the URL (like
/about).HashRouter uses the hash portion of the URL (like
#/about) and is useful for legacy browsers or static file servers that don't support history API fallback.Q5. How do you set up React Router in a project?
Install
Then wrap your app with
Define routes using
Use
react-router-dom.Then wrap your app with
<BrowserRouter>.Define routes using
<Routes> and <Route path="/" element={<Home />} />.Use
<Link to="/about">About</Link> for navigation.