Redux Example - 27th July
Redux Example - 27th July
React182022
React 7:30 PM
Redux Example
Sudhakar Sharma • Jul 27
MVC
- Model -View - Controller
Model
- It represents the data we are working with.
- It contains data logic.
- It contains data validation rules
- It contains meta data [changes and updates]
- It is reffered as "single-source-of-truth"
View
- UI
- It defines view and partial view
- It trigers the actions
Controller
- It comprises actions to perform
- It is reffered as subscriber
- It is a reducer.
- It handles the overall communication between model and view.
User Clicks on "Add to Cart" => Collect the Product Details => Store in Redux Store => Assign to
CartItems[] => Bind to UI
"CartSlicer.tsx"
https://classroom.google.com/u/0/c/NDk2Mjk0NDc1NzI4/m/NTM3NDk0NDY0NjIz/details 1/3
7/31/22, 4:37 PM Redux Example
importReact182022
{ createSlice } from "@reduxjs/toolkit";
React 7:30 PM
const initialState = {
cartItems: [],
cartCount : 0
}
Step-4: Configure UI
th dl AddT C t ( d t ) {
https://classroom.google.com/u/0/c/NDk2Mjk0NDc1NzI4/m/NTM3NDk0NDY0NjIz/details 2/3
7/31/22, 4:37 PM Redux Example
const handleAddToCart = (product:any) => {
React182022
dispatch(addToCart(product));
React 7:30 PM
}
{
products.map(product=>
<card>
<Button onClick={()=> handleAddToCart(product)} className="w-100" variant="contained" >
<span className="bi bi-cart4"></span> Add to Cart</Button>
</card>
)
}
index.tsx
<React.StrictMode>
<Provider store={store} >
<IshopIndexComponent />
</Provider>
</React.StrictMode>,
Redux.docx
Word
Class comments
https://classroom.google.com/u/0/c/NDk2Mjk0NDc1NzI4/m/NTM3NDk0NDY0NjIz/details 3/3