Create a Custom React Hook to Manage Cart State

InstructorColby Fayock

Share this video with your friends

Send Tweet

Often when we have too much business logic in one file, it tends to become hard to read and manage. On top of that, there might be some parts of that logic that we want to abstract so we can use it in other parts of our application.

We can abstract our shopping cart state by creating a custom React hook that we can use in our store to more easily maintain our app. You'll see how much simpler our Home page looks after moving the cart logic out!

~ 3 years ago

Error As:

Unhandled Runtime Error ReferenceError: cartItems is not defined

~ 3 years ago

Why are you importing: import { useState } from 'react' to the Index.js file?

Lucas Minter
~ 3 years ago

import { useState } from 'react'

This is so that the instructor can use this React Hook to set state for his cart.