🌍 ReactJS · subject

ReactJS React Router Syllabus

The React Router syllabus for ReactJS in full: 3 chapters and 9 topics, every one listed below, plus 50 React Router flashcards with answers.

3Chapters
9Topics
50Flashcards

React Router syllabus: all 3 chapters

Expand any chapter to see its topics and sub-topics.

  1. Setting Up React Router

    3 topics
    • Installing React Router
    • Basic Routing
    • Nested Routes
  2. Route Parameters

    3 topics
    • Using URL Parameters
    • Query Parameters
    • Programmatically Navigating
  3. Advanced Routing

    3 topics
    • Protected Routes
    • Redirects
    • Custom Route Components

ReactJS React Router flashcards — 50 questions with answers

  1. Which npm command installs React Router for a web application?

    npm install react-router-dom (this package re-exports everything from react-router plus DOM-specific components like BrowserRouter and Link).

  2. What is the difference between the packages react-router, react-router-dom, and react-router-native?

    react-router holds the core routing logic; react-router-dom adds DOM/web bindings (BrowserRouter, Link, etc.); react-router-native adds bindings for React Native. Web apps install react-router-dom.

  3. Which top-level component must wrap your app to enable routing with the HTML5 history API?

    A router component, typically <BrowserRouter> (or in React Router v6.4+, a router created by createBrowserRouter with <RouterProvider>).

  4. What is the difference between <BrowserRouter> and <HashRouter>?

    <BrowserRouter> uses the HTML5 History API producing clean URLs like /about; <HashRouter> stores the location in the URL hash (/#/about), useful for static file servers that cannot be configured to handle client-side routes.

  5. In React Router v6, which components define a set of routes and an individual route?

    <Routes> wraps the collection, and each <Route path="..." element={<Component/>} /> defines a single route. <Routes> renders only the first best match.

  6. In React Router v6, how do you render a component for a route: which prop is used?

    The element prop, which takes a React element (JSX): <Route path="/home" element={<Home />} />. This replaced v5's component and render props.

  7. What changed from React Router v5's <Switch> to v6?

    <Switch> was replaced by <Routes>. Unlike <Switch>, <Routes> uses a ranking algorithm to pick the best match rather than the first-declared match, and routes no longer need the exact prop.

  8. In React Router v6, are routes matched exactly by default?

    Yes. v6 matches the full path by default, so the exact prop from v5 was removed. A path like /users matches only /users, not /users/5 (unless a nested/child route matches).

  9. Which component replaces the HTML anchor tag for client-side navigation without a full page reload?

    <Link to="/path">. It renders an <a> but intercepts clicks to update the history without reloading the page.

  10. What is <NavLink> and how does it differ from <Link>?

    <NavLink> is a special <Link> that knows whether it is active. It applies an active state so you can style the current route, e.g. via className={({isActive}) => isActive ? 'active' : ''}.

  11. In React Router v6, how does <NavLink> expose its active state for styling?

    Through render-prop callbacks: className, style, and children can each receive an object { isActive, isPending } and return the appropriate value based on isActive.

  12. What is a nested route in React Router?

    A route defined as a child of another route, whose path is relative to the parent. The child component renders inside the parent's <Outlet>, letting UI compose hierarchically (e.g. a layout with nested pages).

  13. Which component marks where a nested/child route should render inside a parent route element?

    <Outlet />. The matched child route's element is rendered wherever the parent places <Outlet /> in its JSX.

  14. How do you write nested routes in JSX in React Router v6?

    Nest <Route> elements: <Route path="dashboard" element={<Dashboard/>}><Route path="stats" element={<Stats/>} /></Route>. The child renders in Dashboard's <Outlet />.

  15. What is an index route and how is it declared?

    An index route is the default child rendered at the parent's exact path when no other child matches. It is declared with the index prop instead of path: <Route index element={<Home/>} />.

  16. What is a layout route (pathless route)?

    A <Route> with an element and children but no path prop. It does not add a URL segment; it exists to share a layout/UI (via <Outlet/>) among its child routes.

  17. How do you define a route that captures a dynamic URL parameter?

    Prefix the segment with a colon: <Route path="/users/:userId" element={<User/>} />. The value matched by :userId becomes a route param named userId.

  18. Which hook reads dynamic URL parameters from the current route?

    useParams(). For a path /users/:userId, const { userId } = useParams() returns the matched value as a string.

  19. In React Router, are URL params returned by useParams strings or their native types?

    Always strings. A numeric id like /users/42 gives userId === '42'; you must convert it (e.g. Number(userId)) yourself.

  20. How do you define an optional or catch-all (splat) segment in a route path?

    A splat/catch-all uses * : <Route path="files/*" element={<Files/>} /> matches files and everything after it; the remainder is available via useParams()['*'].

Show the other 30 React Router flashcards
  1. What are query parameters and how do they differ from route (path) params?

    Query parameters follow the ? in a URL (e.g. ?sort=asc&page=2) and carry optional key/value data; route params are named segments embedded in the path (/users/:id). Query params are not part of route matching.

  2. Which hook reads and updates query parameters (the query string) in React Router v6?

    useSearchParams(). It returns [searchParams, setSearchParams] where searchParams is a URLSearchParams instance.

  3. Given const [searchParams] = useSearchParams(), how do you read the value of ?q=react?

    searchParams.get('q') returns 'react'. It returns null if the parameter is absent. Use getAll('key') for repeated keys.

  4. How do you update the query string with setSearchParams from useSearchParams?

    Call setSearchParams with an object or URLSearchParams, e.g. setSearchParams({ page: '2', sort: 'asc' }). This updates the URL query and re-renders with the new params (a navigation, so it adds a history entry unless replace is set).

  5. Which hook programmatically navigates the user to another route in React Router v6?

    useNavigate(). const navigate = useNavigate(); then navigate('/dashboard') to go to a path.

  6. What replaced the v5 useHistory hook (history.push) in React Router v6?

    useNavigate. Instead of history.push('/x') you call navigate('/x'), and instead of history.replace('/x') you call navigate('/x', { replace: true }).

  7. How do you navigate back and forward in the history stack with useNavigate?

    Pass a number (a delta): navigate(-1) goes back one entry, navigate(1) goes forward one, navigate(-2) goes back two.

  8. How do you replace the current history entry instead of pushing a new one when navigating?

    Pass the replace option: navigate('/login', { replace: true }). This prevents the user from returning to the previous page via the back button.

  9. How do you pass state to a route when navigating programmatically, and how is it read?

    navigate('/path', { state: { from: 'home' } }) attaches state; the destination reads it with useLocation().state (e.g. location.state.from).

  10. Which hook returns the current location object (pathname, search, hash, state)?

    useLocation(). It returns an object with pathname, search, hash, state, and key, and re-renders on navigation.

  11. What is a protected (private) route?

    A route whose access requires a condition (usually authentication). If the user is not authorized, it renders a redirect to a login/other page instead of the protected content.

  12. How do you implement a protected route as a wrapper component in React Router v6?

    Create a component that checks auth and returns either the children (or <Outlet/>) or a <Navigate to="/login" replace />. Example: return isAuth ? <Outlet/> : <Navigate to='/login' replace />.

  13. Which component declaratively performs a redirect when rendered in React Router v6?

    <Navigate to="/path" />. Rendering it immediately changes the location, functioning as a redirect (it replaced v5's <Redirect>).

  14. What replaced the <Redirect> component from React Router v5?

    <Navigate>. Instead of <Redirect to="/login" /> you render <Navigate to="/login" replace />.

  15. Why should a redirect in a protected route use the replace prop on <Navigate>?

    So the redirect replaces the current entry rather than pushing a new one, preventing the user from clicking Back and returning to the protected page they were bounced from.

  16. How can a protected route remember where the user was headed so it can return them after login?

    Capture the current location and pass it in state on the redirect: <Navigate to="/login" state={{ from: location }} replace />. After login, navigate to location.state.from.

  17. How do you create a catch-all 404 route in React Router v6?

    Use a wildcard path: <Route path="*" element={<NotFound/>} />. It matches any URL that no other route matched.

  18. What is the purpose of a custom route component?

    To encapsulate reusable routing logic/behavior (auth checks, role checks, layout wrapping, logging) into a component you place around or in place of standard routes, keeping route definitions clean.

  19. In React Router v6, why do custom wrapper components often render <Outlet/> instead of children?

    When used as a parent route (e.g. <Route element={<RequireAuth/>}>...children...</Route>), the child routes render through <Outlet/>. Rendering children instead is used when the wrapper directly wraps a single element.

  20. What is the difference between an absolute and a relative <Link to> value?

    A leading slash (to="/users") is absolute from the domain root; without it (to="users") it is relative to the current route's path, appending to it.

  21. How do you build links or navigation relative to the parent, going up one level?

    Use .. in the to value: <Link to=".."> navigates up one route segment. In v6 relative resolution is based on the route hierarchy, and you can add relative='path' to resolve against the URL path instead.

  22. What is the data router API introduced in React Router v6.4 and which function creates it?

    The data APIs (loaders/actions) are enabled by createBrowserRouter, used with <RouterProvider router={router} />. This adds data loading, form actions, and error handling to routes.

  23. What is a loader in React Router v6.4+ data routers?

    A function attached to a route (loader) that fetches data before the route's component renders. The component reads it via useLoaderData(), enabling data to be ready on navigation.

  24. Which hook accesses the data returned by a route's loader?

    useLoaderData(). It returns whatever the matching route's loader function returned.

  25. How do loaders perform redirects in React Router data routers?

    By returning (or throwing) a redirect response from the redirect utility: return redirect('/login'). This is the loader-based equivalent of navigating/redirecting.

  26. How do you programmatically read route params inside a loader function (not a component)?

    Loaders receive an argument object with params: loader({ params }) => { params.userId }. Since loaders are not components, you cannot use the useParams hook there.

  27. What order does React Router use when multiple routes could match the same URL in v6?

    It ranks all routes by specificity (static segments beat dynamic :params, which beat splats *) and renders the single highest-ranked match, independent of declaration order.

  28. How do you nest routing so a shared navbar/layout appears on many pages but only the inner content changes?

    Define a parent layout route whose element contains the shared UI plus an <Outlet/>, then nest the page routes as its children so each renders inside the <Outlet/>.

  29. What does the useNavigate returned function do when called with no target but an options object, e.g. navigate(0)?

    navigate(0) refreshes/reloads the current route (re-runs the navigation to the same entry). Numeric deltas move through history; 0 stays on the current entry.

  30. How can you preserve or merge existing query parameters when updating them with setSearchParams?

    Read the current params first and build a new set: setSearchParams(prev => { prev.set('page','2'); return prev; }), or spread existing entries into a new object, since passing a plain object replaces the whole query string.

ReactJS React Router FAQ

What is in the ReactJS React Router syllabus?

ReactJS React Router is split into 3 chapters: Setting Up React Router, Route Parameters and Advanced Routing. Together they contain 9 topics.

How many chapters are in ReactJS React Router?

3 chapters, containing 9 topics. The largest chapter is Setting Up React Router, with 3 topics.

Are there flashcards for ReactJS React Router?

Yes — a 50-card React Router deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.