Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of React Router and navigation in ReactJS, including BrowserRouter, Routes, Route, Link, NavLink, useNavigate, useParams, nested routes, dynamic routing, query parameters, and route protection.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
B. To handle navigation between views based on the URL
React Router maps URLs to components or route elements, allowing users to navigate between views in a React application.
A. Routes
In modern React Router declarative routing, the Routes component contains Route definitions and renders the best matching route.
C. Link
The Link component from React Router enables client-side navigation between routes without a normal full-page browser navigation.
D. useNavigate
The useNavigate hook returns a function that can navigate to another location or move through the browser history.
B. useParams
The useParams hook returns an object containing the matched dynamic route parameters, such as an ID from a path like /users/:id.
A. To synchronize routing with the browser's URL using the History API
BrowserRouter uses the browser History API to manage client-side navigation and keep the rendered UI synchronized with the URL.
C. NavLink
NavLink supports active-state styling and can expose whether a link is active or pending, making it useful for navigation menus.
D. To organize child routes within a parent route and share layout structure
Nested routes allow child views to render within a parent layout, commonly using the Outlet component as the rendering location.
B. Outlet
The Outlet component renders the matched child route element within the parent route's layout.
A. useLocation
The useLocation hook returns the current location object, including fields such as pathname, search, hash, and state.
C. To match paths that do not match more specific route definitions
A wildcard route is commonly used as a fallback for unmatched URLs, such as to display a not-found page.
D. Navigate
The Navigate component changes the current location when rendered. The older Redirect component belonged to earlier React Router versions.
B. useSearchParams
The useSearchParams hook provides access to URL search parameters and a setter for updating them.
A. They can conditionally render content or redirect based on authentication or authorization state
Protected route patterns can control client-side access to views, but sensitive data and operations must also be protected by server-side authorization.
C. Link typically performs client-side navigation, while a standard anchor normally triggers browser navigation
React Router's Link handles internal navigation within the application. A standard anchor usually initiates normal browser navigation and may reload the document.