- Handling Async Tasks With Redux
- Where To Put Your Code
- The Redux DevTools
- add a
README.md
file - run
npm install
- run
npm start
- add a
.gitignore
file & addnode_modules
inside of it
- run
npm install @reduxjs/toolkit
- run
npm install react-redux
- set up the Redux logic & connect it to the React app
- add a
store
folder & inside aindex.js
file to set up the Redux store and so on - create multiple slices: one
cart-slice.js
file for managing the cart & oneui-slice.js
for the user interface logic - use
createSlice()
to create the different slices & export them as well as their actions - create the store in
store/index.js
with help ofconfigureStore
& set up the root reducer inside of it & export it - provide the exported store to the application to make effect in the
src/index.js
file with help of the<Provider>
component
- add a
- in
CartButton.js
, implement the logic to toggle the cart with help ofuseDispatch
& theuiActions
- in
App.js
, render the<Cart>
component conditionally based on theuiSlice
value that you extract with help ofuseSelector
- updating the cart items content when we click the
Add to Cart
, the+
&-
and the cart badge buttons- create the
cartSlice
with help ofcreateSlice
incart-slice.js
- set up the
cartSlice
logic
- create the
- set up the logic for the
removeItemFromCart
function incart-slice.js
- export the
cartSlice
as default - export the
cartActions
- in
store/index.js
, merge thiscartSlice
into the overall Redux store - wire up the cart actions
- add a
DUMMY_PRODUCTS
array inProducts.js
& set the properties based on the properties incart-slice.js
- in
ProductItem.js
, wire theAdd to Cart
button to theaddItemToCart
function incart-slice.js
- update the cart badge in
CartButton.js
by reading the cart slicetotalQuantity
with help ofuseSelector()
- render the cart correctly in
Cart.js
by reading the cart sliceitems
with help ofuseSelector()
- make the
+
&-
buttons work inCartItem.js
with help ofuseDispatch()
&cartActions
- add a
- create a new project in Firebase named
react-redux-cart
- keep the link to where you will send your HTTP requests
- get hold of the overall cart with help of
useSelector()
inApp.js
- use
useEffect()
to watch for changes in the cart state - inside of it, send a HTTP request to Firebase
- add the new
Notification.js
file inside theUI
folder - use this file to show the notification & handle errors in the
useEffect()
function inApp.js
- manage the notification with help of Redux by adding a
notification
property to theinitialState
& ashowNotification
method - dispatch the notification when starting sending the data, the data is sent successfully & if there is an error in
App.js
- use this new
notification
UI state fromui-slice.js
with help ofuseSelector()
to render the<Notification>
component conditionally inApp.js
- in
App.js
, make sure that you don't send the cart when the app runs for the first time with help of anisInitial
variable
- move the code for sending cart data out of the
App.js
component into an action creator - create a new
sendCartData
action creator incart-slice.js
- dispatch the
sendCartData
action inside theuseEffect()
function inApp.js
- move the
sendCartData
action creator out ofcart-slice.js
into a newcart-actions.js
file - build another
fetchCartData
action creator that fetches the cart when the application loads incart-actions.js
- add a new
replaceCart
function inside of thecartSlice
function incart-slice.js
- dispatch the
cartData
with help of thereplaceCart()
from thecartActions
inside thefetchCartData
action creator incart-actions.js
- call
fetchCartData
inside a newuseEffect
function inApp.js
- fix the issue where we automatically resend the cart when the application loads
- add a new
changed
property in thecartSlice
function incart-slice.js
- dispatch
sendCartData
conditionally inApp.js
- in
cart-actions.js
, send a new object without thechanged
property instead of the overallcart
to Firebase
- add a new
- fix the bug where when you clear the cart entirely then reload and add an item to cart again or show the cart again
- when the cart is cleared entirely, there is no
items
key anymore in Firebase - in
cart-actions.js
, where we replace the cart withcartData
, transform thecartData
so that the payload is an object that always has anitems
key
- when the cart is cleared entirely, there is no