【发布时间】:2019-10-19 14:35:04
【问题描述】:
我正在尝试构建一个 react redux typescript 应用程序。
我正在使用 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;
也有例子就好了。
【问题讨论】:
标签: javascript reactjs typescript redux