Note: we'll use the results of this homework assignment for a formal evaluation and as such you should write the code individually. Plagiarism is a violation of the Academy contract and makes nobody happy.
We will display a list of products with prices. We will be asking you to apply several skills you've learned this week.
This assignment is split into steps. Each successfully completed step will contribute to your final score. Try to complete as many of them as you can.
Note: You can implement some of them out of order!
Send your homework to teachers@codaisseur.com before Sunday 20:00
Clean up your code:
- Fix the formatting/indentation
- Remove unnecessary code.
The app should be runnable simply by cloning the repository and running yarn start
Use the method you've learned to create a new empty React application.
Define two components in your project and name them:
CheckoutButton
: Should render a<button>
elementCartItem
: Should render<li>
element
Given the following data, render a list of CartItem
s inside your App
component. Pass the data to the CartItem
component as JSX props. Display both the name and price for each of them. Render the list inside of a <ul>
.
Hint: Use the array itself as the source of your data. Do not simply copy (hard-code) the values.
const products = [
{
id: 1,
name: "Prada Shoes",
price: 570
},
{
id: 2,
name: "Rolex Watch",
price: 649
},
{
id: 3,
name: "Paper clips",
price: 0.1
}
]
In your App
component, define a method on the class with the name incrementQuantity
. It should take one parameter: productId
. We will implement the functionality later.
Add a prop to the rendered CartItem
s called onPlusClick
. The value of the prop should be a function that calls the incrementQuantity
method with the id
of the product being rendered.
Hint: Remember that a method is a function on an object. So you need a reference to the object in order to call the function.
Hint 2: Use an anonymous function as the value of
onPlusClick
, so you can pass the appropriate id.
After completing the previous step, you will now have a new prop inside your CartItem
. Add a button to the rendered output. When that button is clicked it should call the function in the prop onPlusClick
.
When called, this method should update the quantity for the given product in the App
's local state using setState
. Each time the quantity should increase by one relative to its previous value.
Now also add the quantity to the CartItem
so that it's displayed next to the price.
Hint: this means your
App
component will need to have (in its local state) a collection of products and their associated quantities. You can initialize this state in the constructor, using the provided data in the previous step. The initial quantity for each product is0
.
Add the following package as a dependency to your project: react-live-clock
Read the react-live-clock
documentation on how to use it. Render it at the top of your App
.
Similar to how you passed a function to the CartItem
s, now pass a function to the CheckoutButton
.
When the button is clicked, the total value of the shopping cart should be displayed on the page.
Define the propTypes for all props that your components expect/use.
With luck and hard work your app will look like this: