An open API service indexing awesome lists of open source software.

https://github.com/phukon/shopping-cart-react

React Context-Api practice in Typescript
https://github.com/phukon/shopping-cart-react

react-context-api

Last synced: 7 months ago
JSON representation

React Context-Api practice in Typescript

Awesome Lists containing this project

README

          

# React Context + Typescript Practice

I wanted to brush up my React + Typescript skills and what better way to do so
than by delving into contexts and diving deep into the data flow?


store screenshot

---
- Used best practices for using Context (abstraction using custom hooks)
- Items are listed in the `/store` route.
- Items can be added doing which the cart `🛒` icon with the total number of items in the cart appears.
- This cart `🛒` icon can be clicked to reveal a sidebar that gives the summary of the items, prices and quantities.
- A navigation bar and three routes

## Nuances

Writing types was the fun part. Here are the types I wrote for the `Shopping Cart Context`

### Types

```typescript
type TShoppingCartProviderProps = {
children: ReactNode;
};

type TShoppingCartContext = {
openCart: () => void;
closeCart: () => void;
getItemQuantity: (id: number) => number;
increaseCartQuantity: (id: number) => void;
decreaseCartQuantity: (id: number) => void;
removeFromCart: (id: number) => void;
cartQuantity: number;
cartItems: TCartItem[];
};

type TCartItem = {
id: number;
quantity: number;
};
```