Skip to content

Commit 479a596

Browse files
updating our application with reselect and replacing cart selectors with memoized selectors
1 parent 52e5d09 commit 479a596

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"react-router-dom": "^5.2.0",
1212
"react-scripts": "2.1.8",
1313
"redux": "^4.0.5",
14-
"redux-logger": "^3.0.6"
14+
"redux-logger": "^3.0.6",
15+
"reselect": "^4.0.0"
1516
},
1617
"scripts": {
1718
"start": "react-scripts start",

src/components/cart-dropdown/cart-dropdown.component.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import CartItem from '../cart-item/cart-item.component'
33
import { connect } from 'react-redux'
44
import CustomButton from '../custom-button/custom-button.component'
55
import './cart-dropdown.styles.scss'
6+
import { selectCartItems } from '../../redux/cart/cart.selectors'
7+
8+
9+
10+
611
const CartDropdown = ({ cartItems }) => (
712
<div className="cart-dropdown">
813
<div className="cart-items">
@@ -15,8 +20,8 @@ const CartDropdown = ({ cartItems }) => (
1520

1621

1722

18-
const mapStateToProps = ({ cart: { cartItems } }) => ({
19-
cartItems
23+
const mapStateToProps = (state) => ({
24+
cartItems: selectCartItems(state)
2025
})
2126

2227
export default connect(mapStateToProps)(CartDropdown)

src/components/cart-icon/Cart-icon.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import { connect } from 'react-redux'
3-
3+
import { selectCartItemsCount } from '../../redux/cart/cart.selectors'
44
import { toggleCartHidden } from '../../redux/cart/cart.actions'
55

66
import './cart-icon.styles.scss'
@@ -20,8 +20,8 @@ const mapDispatchToProps = dispatch => ({
2020
})
2121

2222

23-
const mapStateToProps = ({ cart: { cartItems } }) => ({
24-
itemCount: cartItems.reduce((accumulatedQuantity, cartItem) => accumulatedQuantity + cartItem.quantity, 0)
23+
const mapStateToProps = (state) => ({
24+
itemCount: selectCartItemsCount(state)
2525
})
2626

2727
export default connect(mapStateToProps, mapDispatchToProps)(CartIcon)

src/redux/cart/cart.selectors.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createSelector } from 'reselect'
2+
3+
const selectCart = state => state.cart
4+
5+
6+
export const selectCartItems = createSelector(
7+
[selectCart],
8+
cart => cart.cartItems
9+
)
10+
11+
12+
export const selectCartItemsCount = createSelector(
13+
[selectCartItems],
14+
cartItems =>
15+
(cartItems.reduce(
16+
(accumulatedQuantity, cartItem) =>
17+
accumulatedQuantity + cartItem.quantity,
18+
0
19+
)
20+
)
21+
)

0 commit comments

Comments
 (0)