0
0
Remixframework~10 mins

Dynamic route parameters in Remix - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a dynamic route parameter in Remix.

Remix
export default function Post() {
  const params = useParams();
  return <div>Post ID: {params.[1]</div>;
}
Drag options to blanks, or click blank then click option'
Aid
BpostId
Cslug
Dparam
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not match the file name.
Trying to access params without using useParams hook.
2fill in blank
medium

Complete the code to fetch the dynamic parameter from the loader function.

Remix
export async function loader({ params }) {
  const postId = params.[1];
  return json({ postId });
}
Drag options to blanks, or click blank then click option'
Aslug
Bid
CpostId
Dparam
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not match the route file.
Trying to destructure params incorrectly.
3fill in blank
hard

Fix the error in the route file to correctly define a dynamic route parameter.

Remix
import { useParams } from '@remix-run/react';

export default function User() {
  const params = useParams();
  return <div>User: {params.[1]</div>;
}

// The file is named users.$userId.jsx
Drag options to blanks, or click blank then click option'
AuserId
Bid
Cuser
Dparam
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic 'id' instead of the specific parameter name.
Not importing useParams from Remix.
4fill in blank
hard

Fill both blanks to create a loader that fetches data using the dynamic parameter and returns JSON.

Remix
export async function loader({ [1] }) {
  const id = [2].id;
  const data = await fetchDataById(id);
  return json(data);
}
Drag options to blanks, or click blank then click option'
Aparams
Brequest
Dcontext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' instead of 'params' to get route parameters.
Trying to access id directly without params.
5fill in blank
hard

Fill all three blanks to create a Remix route component that uses the loader data and dynamic parameter.

Remix
import { useLoaderData, useParams } from '@remix-run/react';

export default function Post() {
  const data = useLoaderData();
  const [1] = useParams();
  return (
    <main>
      <h1>Post ID: [2].id</h1>
      <p>{data.content}</p>
    </main>
  );
}

export async function loader({ [3] }) {
  const post = await getPostById([3].id);
  return json(post);
}
Drag options to blanks, or click blank then click option'
Aparams
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for params in component and loader.
Trying to access id without params.