Я пытаюсь создать приложение с поддержкой машинного редукса.
Я использую библиотеку reslect для создания селекторов.
Я получаю сообщение об ошибке при использовании createStructuredSlectore. Пожалуйста, найдите ошибку ниже.
Ошибка: перегрузка не соответствует этому вызову
Пожалуйста, найдите код ниже:
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { toggleDropDown } from '../../redux/cart/cart.actions';
import { selectItemCount } from '../../redux/cart/cart.selectors';
import { ReactComponent as ShoppingIcon } from '../../assets/11.2 shopping-bag.svg.svg';
import './cart-icon.component.scss';
interface ICartIconProps {
toggleDropDown: () => void;
itemCount: number;
}
const CartIcon: React.FC<ICartIconProps> = ({toggleDropDown, itemCount}) => (
<div className='cart-icon' onClick={toggleDropDown}>
<ShoppingIcon className='shopping-icon'/>
<span className='item-count'>{itemCount}</span>
</div>
)
const mapDispatchToProps = (dispatch: import('redux').Dispatch) => ({
toggleDropDown: () => dispatch(toggleDropDown())
})
// If I use Below code its working fine
// const mapStateToProps = (state: AppState) => ({
// itemCount: selectItemCount(state)
// })
// Iam getiing error here with below code
const mapStateToProps = createStructuredSelector({
itemCount: selectItemCount,
})
export default connect(mapStateToProps, mapDispatchToProps)(CartIcon);
Slectors.ts
import { createSelector } from 'reselect'
import { AppState } from '../store'
import { ShopItem } from '../../models/collection.model'
const selectCart = (state: AppState) => state.cart
export const selectCartItems = createSelector(
[selectCart],
cart => cart.cartItems
);
export const selectHidden = createSelector(
[selectCart],
cart => cart.hidden
);
export const selectItemCount = createSelector(
[selectCartItems],
(cartItems: ShopItem[]) => {
return cartItems.reduce(
(accumulatedValue, cartItem) =>
accumulatedValue + (cartItem.quantity as number),
0
)
}
);
cart.reducer.ts
import { CartActionsTypes } from "./cart.types";
import { addItemToCart } from "./cart.utils";
const INITIAL_STATE = {
hidden: true,
cartItems: []
};
const cartReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case CartActionsTypes.TOGGLE_CART_DROPDOWN:
return {
...state,
hidden: !state.hidden
}
case CartActionsTypes.ADD_ITEM:
return {
...state,
cartItems: addItemToCart(state.cartItems, action.payload)
}
default:
return state;
}
}
export default cartReducer;
также хорошо иметь пример.