Week 6 Assignment
Overview
On this week's assignment, you will be merging the functionalities developed during Week 4 and Week 5 into a single, interactive shopping list application. The application will consist of a form for adding new items (NewItem
component from Week 4) and a sortable list for displaying these items (ItemList
and Item
components and items.json
file from Week 5).
Key Learning Objectives
- Component Composition: Learn how to create complex interfaces by combining simpler components.
- Data Flow: Understand how to manage and pass data between components in a multi-component application.
- React State Management: Consolidate your understanding of the useState hook to manage application state across multiple components.
Prerequisites
Complete Week 4 Assignment and Week 5 Assignment. If you did not complete the Week 4 and Week 5 assignments, you can use another student's solution by copying the files from their GitHub repository into your own.
Optional Challenge: Minimal Instructions
If you're up for a bit of a challenge, try tackling this assignment without the comfort of detailed steps. Instead, use your knowledge of React fundamentals to combine together functionalities from past assignments into a cohesive application. Your goal? A dynamic shopping list that allows users to add items and view them in a sortable list. To make this happen, give thought to how state management can handle data movement across your application. Best of luck!
Detailed Instructions
Setup Files
Let's first create a new folder for this assignment and populating it with the files from previous assignments to use as a starting point.
- Create a new folder called
week-6
inside yourapp
folder. - Copy
item-list.js
,item.js
,items.json
, andpage.js
from /week-5 to /week-6. - Copy
new-item.js
from /week-4 to /week-6.
Modify page.js
Next, we're going to update page.js
to serve as the orchestrator of our shopping list app. This involves integrating the imported components, setting up state management for the items, and establishing the event handler for adding new items to the list.
- Import the
NewItem
andItemList
components. - Import
items.json
asitemsData
. - Initialize a state variable (e.g.,
items
) with the data fromitems.json
. - Create an event handler function (e.g.,
handleAddItem
) that adds a new item toitems
. - In your render function, display both the NewItem and ItemList components.
- Pass the
handleAddItem
event handler to theNewItem
component as a prop calledonAddItem
. - Pass the
items
state to theItemList
component as a prop.
- Pass the
- Add the "use client" directive at the top of the file.
Modify new-item.js
Next, our attention shifts to new-item.js
. We'll be enhancing this component by introducing a new prop and redefining its behavior upon form submission. Rather than triggering an alert, it will now invoke the onAddItem prop, passing along the item object.
- Add a new prop
{ onAddItem }
. - Replace the alert functionality by calling the onAddItem prop with the item object when the form is submitted. The item object should have the following properties:
name
,quantity
, andcategory
.
Modify item-list.js
Let's now turn our focus to the item-list.js
file. We're going to modify it to receive data via props and revise all functions that were previously altering the list directly. Instead, they should operate on a copy of the prop to ensure we're adhering to the principle of immutability in React.
- Remove the import statement for
items.json
since the data is now being passed as a prop. - Add a new prop
{ items }
. - Replace all functions that mutate the items prop with versions that do not mutate the prop. Instead, these functions should create a copy of items, perform any necessary operations on the copy, and use this copy for rendering.
item.js
does not need to be modified.
Test Your Application
Cross your fingers and run npm run dev
to see if your application works as expected.
- Ensure that adding a new item with the
NewItem
form adds the item to theItemList
. - Make sure the
ItemList
maintains its sorting functionality from Week 5.
Fix All Warnings
Now, it's time for some housekeeping. We need to ensure our application is free of warnings which might indicate potential issues.
- Fix all warnings that appear in the browser console and in the terminal. To view warnings in the browser, open the developer tools and click on the "Console" tab. To view warnings in the terminal, look for any warnings that appear in the terminal window where you ran the command
npm run dev
.
Example Output
https://cprg306-assignments.vercel.app/week-6 (opens in a new tab)
Assignment Submission
The instructor will be able to find your assignment in your GitHub repository. Make sure you have committed and pushed your changes to GitHub before the assignment deadline.
Check your GitHub account to see if your assignment has been correctly pushed.