0
0
Reactframework~20 mins

Route parameters in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 />} />
AUser ID: 42
BUser ID: id
CUser ID: undefined
DError: useParams is not a function
Attempts:
2 left
💡 Hint
Remember that useParams returns an object with keys matching the route parameters.
📝 Syntax
intermediate
2: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?
A<Route path="/product/:id" element={<Product />} />
B<Route path="/product/*id" element={<Product />} />
C<Route path="/product/{id}" element={<Product />} />
D<Route path="/product/id" element={<Product />} />
Attempts:
2 left
💡 Hint
Route parameters use a colon before the name.
state_output
advanced
2: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 />} />
APost ID: undefined
BPost ID: 1
CPost ID: NaN
DPost ID: 2
Attempts:
2 left
💡 Hint
The effect updates the state when the parameter changes.
🔧 Debug
advanced
2: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 />} />
AThe route path is missing a colon before userId.
BThe state is initialized once and does not update when userId changes.
CThe component should use useEffect to fetch data.
DuseParams does not return userId correctly.
Attempts:
2 left
💡 Hint
Think about how useState works with initial values.
🧠 Conceptual
expert
3: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>;
}
AuseParams returns parameters only if explicitly passed as props.
BuseParams returns only the parameters from the current route segment.
CuseParams returns all parameters from parent and child routes combined.
DuseParams cannot be used in nested routes.
Attempts:
2 left
💡 Hint
React Router merges parameters from all matched routes.