React Router DOM Lab Exercise
Overview
You are contacted by a client as a frontend developer to build the frontend of a system for Product Management for a grocery store. Another developer has built the backend API and wrote some basic documentation for it. This is an internal website as is for the employees and is not customer facing You need to build a frontend for this API. Check out the API Documentation here: API Documentation Link Here
You will build the following components in this lab:
- App: The root component that orchestrates the entire application.
- Homepage: The homepage of our application. Should welcome the user to the app.
- NavBar: Provides navigation links.
- CreateProduct: Allows users to enter details for and submit new products.
- ProductsList: Displays an index list of all products.
- ProductsDetails: Shows details about a specific product.
Lab exercise
To get started, follow the steps below:
-
Go to this URL to read the documentation on the API you will use for this lab: API Documentation Link Here. READ THE DOCUMENTATION to understand how it works
-
Test the API in postman to see how it looks. Make API calls to all 3 endpoints
-
Start by creating each of the components listed below. After creating each component, be sure to import them at the top of
src/App.jsx.src/components/NavBar/NavBar.jsxsrc/pages/CreateProduct/CreateProduct.jsxsrc/pages/ProductList/ProductList.jsxsrc/pages/ProductDetails/ProductDetails.jsxsrc/pages/ProductDetails/Homepage.jsx
-
Within
NavBar.jsx, importLinkfromreact-routerand create a<Link>for each destination in the table below:Destination toText Content Home /Home ProductsList /productsProducts CreateProduct /new-productNew Products -
In
App.jsx, importRouteandRoutesfromreact-routerand create a<Route>for each destination in the table below:Destination pathelementHome /<Homepage/>ProductsList /products<ProductsList />CreateProduct /new-product<CreateProduct />ProductsDetails /products/:productId<ProductsDetails /> -
Use the following API to READ THE DOCUMENTATION. And in the page components add the API calls:
-
In the
/new-productroute create a form. It should accept form data for a new product and POST the values to the API -
In the
/productsan API call should be made to get all the products -
In the
/products/:productIdroute it should show the specific product -
If you haven’t already, create a .env file and add the Base URL inside. Make sure to make the calls with the
import.meta.envinstead of the url. We dont want to expose the API URL
REMEMBER : This is Omar’s free API so it will have a slow start up if inactive for long time
Bonus
- Use antD package to add spinners in your app for loading
- Add try catch to all your API calls
- Create a service file that contains all the api calls. Export all the functions and call them in the components
- Redirect the user to the
ProductDetailspage instead of theProductsList - Create an error state that is set in the .catch() of your api calls so the user sees a clear error message. So now your component can be in 1 of 3 states:
- Loading
- Error
- displaying values