Learn to implement dynamic routing for a product details page in a React application. Create a product listing where each product links to a unique detail page based on the product ID, using data from a JSON file.
-
Create an 'Exercises' Directory (If not already done):
- If you haven't already, create a main directory on your local machine to house all your exercises. You can do this using
mkdir Exercises
.
- If you haven't already, create a main directory on your local machine to house all your exercises. You can do this using
-
Fork the Repository:
- Navigate to the GitHub repository for this exercise at
03-23-dynamic-product-details
. - Click on the 'Fork' button to create a fork of the repository.
- Navigate to the GitHub repository for this exercise at
-
Clone Your Forked Repository:
- Open your terminal.
- Navigate to your
Exercises
directory usingcd path/to/Exercises
. - Run
git clone <your-forked-repo-url>
to clone the repository to your machine. - This will create a new directory named
03-23-dynamic-product-details
underExercises
.
-
Initial Setup:
- The repository already includes the skeleton code generated by Create React App, along with a
data
folder in thesrc
directory containing theproducts.json
file. - Navigate to the exercise directory:
cd 03-23-dynamic-product-details
. - Run
npm install
to install the necessary dependencies. - Start the development server with
npm start
.
- The repository already includes the skeleton code generated by Create React App, along with a
The products.json
file in the data
folder will have the following structure:
{
"products": {
"1": {
"name": "Product One",
"description": "Description for Product One",
"price": "19.99"
},
"2": {
"name": "Product Two",
"description": "Description for Product Two",
"price": "29.99"
},
// ... more products
}
}
Each product is an object with properties like name
, description
, and price
, keyed by productId
.
-
Create a Product Listing Page:
- Build a
ProductList
component that displays a list of products. Use theproducts.json
file as the source of product data. - Display each product's name in the list, and each product name should be a link to its detailed page.
- Build a
-
Implement Dynamic Routes for Product Details:
- Set up dynamic routes in your application so that clicking a product name in the
ProductList
navigates to the correspondingProductDetail
page. - The route for each product detail page should be
/product/:productId
. - Create a
ProductDetail
component that retrieves and displays the details of the product fromproducts.json
based on theproductId
obtained from the URL.
- Set up dynamic routes in your application so that clicking a product name in the
-
Hint for Accessing Product Details:
- In the
ProductDetail
component, use theuseParams
hook fromreact-router-dom
to extract theproductId
from the URL. - Use this
productId
to access the corresponding product's details from theproducts.json
file.
- In the
- Ensure that clicking on a product in the
ProductList
navigates to theProductDetail
page. - Verify that the
ProductDetail
page correctly displays the information for the product based on theproductId
in the URL.
-
Commit Your Changes:
- After completing the exercise, ensure all your changes are saved.
- Run
git add .
to stage your changes. - Run
git commit -m "Completed Dynamic Product Details Page exercise"
to commit your changes.
-
Push to Your Fork:
- Run
git push
to push your commits to your forked repository on GitHub.
- Run
-
Create a Pull Request:
- Go to your forked repository on GitHub.
- Click on 'Pull Request' to create a new pull request to the original repository.
- Ensure the pull request details are filled out, explaining the changes you made.