Challenge - 5 Problems
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing route parameters?
Consider a React Router setup where a component reads a route parameter. What will be rendered when the URL is
/user/42?React
import { useParams } from 'react-router-dom'; function User() { const { id } = useParams(); return <div>User ID: {id}</div>; } // Route: <Route path="/user/:id" element={<User />} />
Attempts:
2 left
💡 Hint
Remember that useParams returns an object with keys matching the route parameters.
✗ Incorrect
The useParams hook extracts the dynamic parts of the URL defined by the route. Here, :id matches 42, so id is '42'.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a route with a parameter?
You want to create a route that matches URLs like
/product/123 where 123 is a dynamic product ID. Which route definition is correct?Attempts:
2 left
💡 Hint
Route parameters use a colon before the name.
✗ Incorrect
In React Router, dynamic segments are defined with a colon, like ':id'.
❓ state_output
advanced2:00remaining
What is the value of the state after route parameter change?
Given the component below, what will be the displayed output after navigating from
/post/1 to /post/2?React
import { useParams } from 'react-router-dom'; import { useState, useEffect } from 'react'; function Post() { const { postId } = useParams(); const [id, setId] = useState(postId); useEffect(() => { setId(postId); }, [postId]); return <div>Post ID: {id}</div>; } // Routes: <Route path="/post/:postId" element={<Post />} />
Attempts:
2 left
💡 Hint
The effect updates the state when the parameter changes.
✗ Incorrect
The useEffect hook runs when postId changes, updating the state to the new postId value.
🔧 Debug
advanced2:00remaining
Why does this component not update on route parameter change?
This component reads a route parameter but does not update when the URL changes. What is the cause?
React
import { useParams } from 'react-router-dom'; import { useState } from 'react'; function Profile() { const { userId } = useParams(); const [id] = useState(userId); return <div>Profile ID: {id}</div>; } // Route: <Route path="/profile/:userId" element={<Profile />} />
Attempts:
2 left
💡 Hint
Think about how useState works with initial values.
✗ Incorrect
useState sets the initial value only once. Changes in userId do not update the state unless explicitly handled.
🧠 Conceptual
expert3:00remaining
How does React Router handle multiple route parameters in nested routes?
Given nested routes with parameters, how are parameters accessed in child components?
React
/* <Route path="/category/:catId" element={<Category />}> <Route path="product/:prodId" element={<Product />} /> </Route> */ function Product() { const params = useParams(); return <div>{`Category: ${params.catId}, Product: ${params.prodId}`}</div>; }
Attempts:
2 left
💡 Hint
React Router merges parameters from all matched routes.
✗ Incorrect
useParams returns an object with all parameters from the matched route hierarchy, including parents.