In this module you will take your newfound knowledge of Context API and refactor a e-commerce store to use Context API as well as extend the functionality of the application making it more robust!
Read these instructions carefully. Understand exactly what is expected before starting this project.
Commit your code regularly and meaningfully. This helps both you and your team lead in case you ever need to return to old code for any number of reasons.
In this project you'll take take an almost completed e-commerce store and refactor the application to use the Context API.
- Download project dependencies by running
npm install. - Start up the app using
npm start.
Before you get started, please take a few minutes and get acquainted with this application. Understand what's going on and how it's working.
-
If you look in the
App.jsyou'll notice there are currently two state properties -productsto keep track of all available products, andcartthat will keep track of all the items in ourcart. -
You'll also notice inside of our
App.jswe have 3 components. A navigation component and two route based components. Each of those components are all being passed either ourcartstate orproductstate as props, when we start to scale our application and add more props our codebase is going to start to become very cumbersome and will make our application hard to work with. -
To combat this from happening we're going to refactor our application to use
Context API, making it easier and more effiecent to access data across our application.
Step 1 - Add item functionality
- In
App.jsthere is a function calledaddItem. Finish writing the logic in this function to be able to add the given item to the shopping cart
STEP 2 - Creating ProductContext
-
In
src, create a new folder namedcontexts, this folder is going to be used to hold all ofcontext objectswe create. -
Inside that folder create a new file named
ProductContext.js -
In this file, import the
createContextfunction from the react library and create ourProductContext.
STEP 3 - Providing data with ProductContext
-
Now that we've created our
ProductContextwe can import into ourApp.js. Now we can start providing data across our application! -
Wrap all of your components/routes in
App.jsinside ofProductContext.Providercomponent. -
Next pass a value prop to your
Provider. -
In the value prop we'll pass in the products state, and an addItem function that will allow us to add books to the cart.
<ProductContext.Provider value={{ products, addItem }}>- Now that we're providing our products state and addItem function we can simplify our products route a bit.
Before
<Route exact path="/">
<Products products={products} addItem={addItem} />
</Route>After
<Route exact path="/">
<Products />
</Route>- After refactoring you'll notice a few errors... Don't worry we'll clean those up shortly!
STEP 4 - Consuming data with ProductContext
-
Now that our
ProductContextis now providing data we can finally consume it! To do so let's head over to ourProductscomponent and import theuseContexthook as well as ourProductContext. -
In the component, call the
useContexthook and pass in the context object we want to use into it. -
When we do this,
useContextis going to return value passed by ourProductContextProvidervalueprop. In our case we're getting back an object with two properties. Aproductsproperty and aaddItemproperty. We can go ahead and destructure those.
const { products, addItem } = useContext(ProductContext);-
Now that we have all of the data we need we can refactor our
Productscomponent from using props. -
To do so we just need to remove every instance of
props.- Remove it from the function parameters
- Remove it from the products map
- Remove it from addItem prop
-
Now our
Productscomponent is getting it's data solely fromContext API😃.
STEP 5 - Create the CartContext
-
Now that we have refactored our
Productscomponent to utilizeContext APIlet's refactor ourCartandNavigationComponent to useContext APIas well. -
To start create a new file in our contexts folder named
CartContext.js, this context is going to be utilized by ourShoppingCartandNavigationcomponent. -
Inside of our new
CartContextimportcreateContextand create a new context namedCartContext.
STEP 6 - Providing data with CartContext
-
Let's go ahead and bring our newly created
CartContextinto ourApp.jsand wrap all of our components inside of ourCartContext.Provider. Make sure ourProductContext.Provideris still the root provider. -
Now pass a value prop to our
CartContext.Provider, this value prop is going to contain ourcartstate. -
Now that we're providing our cart data, we can start to refactor our
NavigationandShoppingCartcomponents. -
Let's start with our
ShoppingCartcomponent first. Go ahead and refactor theShoppingCartroute to no longer use render props. This will throw us an error, but we'll be able to resolve it quickly. -
While were at it let's go ahead and remove the props from our navigation as well.
STEP 7 - The final stretch
-
Our cart data is now being provided to us from our
CartContexttime to consume it! -
First, let's head to our
ShoppingCartcomponent and import theuseContexthook and ourCartContext. -
Now in the component, pass
CartContextto theuseContexthook and assign it to a variable named cart. -
Inside of our component we now need to remove all instances of props.
- Remove the
propsparameter - Remove the
propsportion in ourgetCartTotalfunction - Remove
propswhen we're mapping over our cart
- Remove the
-
Time to do the same thing for our
Navigationcomponent.- First import the
useContexthook and ourCartContext - Next, pass our
CartContextto theuseContexthook and assign it to a variable named cart. - Lastly we need to remove all instances of
props- Remove
propsfrom our parameters - Remove
propsfrom our cart length
- Remove
- First import the
We have now successfully converted our application into using Context API 🔥
MVP Requirements
- Create a
ProductContextand aCartContext - Use the Provider Component from
ProductContextandCartContextto provide data to child components - Consume data using the
useContexthook fromProductContextandCartContext
Do not attempt stretch problems until MVP has been reached and a final commit has been made.
-
Create a
removeItemfunction that allows you to remove an item from your cart with a click of a button. ThisremoveItemfunction should be able to be consumed from yourShoppingCartItemcomponent. Remember each item has anidthis will help out a lot while creating your removeItem function! -
Persist Cart Items using
localStorage. (If you try this one, it will be a bit tricky to get our items to populate the shopping cart on a refresh. You'll have to think about where the data actually lives, and how you can get data there from localStorage when the app is being mounted after a refresh. Good luck!)
- If this is your first time connecting a submission, authorize your github account within the codegrade assignment.
- Connect your fork to Codegrade using the "Connect Git" button.
- Find your newly created fork from the list and push your work to main.
- Check this video for details: www.youtube.com/watch?v=fC2BO7dI6IQ