🎈 perf(购物页面): 优化购物页面的删除功能
This commit is contained in:
parent
41dd477f2e
commit
c0d3f61806
@ -1,7 +1,7 @@
|
|||||||
import { View, Text, Image } from '@tarojs/components'
|
import { View, Text, Image } from '@tarojs/components'
|
||||||
import MCheckbox from '@/components/checkbox'
|
import MCheckbox from '@/components/checkbox'
|
||||||
import Counter from '@/components/counter'
|
import Counter from '@/components/counter'
|
||||||
import { FC, forwardRef, memo } from 'react'
|
import { FC, forwardRef, memo, useCallback } from 'react'
|
||||||
import classnames from 'classnames'
|
import classnames from 'classnames'
|
||||||
import styles from './index.module.scss'
|
import styles from './index.module.scss'
|
||||||
import { debounce } from '@/common/util'
|
import { debounce } from '@/common/util'
|
||||||
@ -11,6 +11,7 @@ import { selectList } from '../../config'
|
|||||||
import { AdjestShoppingCartApi } from '@/api/shopping/index'
|
import { AdjestShoppingCartApi } from '@/api/shopping/index'
|
||||||
import { Goods, ShoppingDispatchType, ShoppingStateContextValue, useShoppingDispatch, useShoppingState } from '../../context'
|
import { Goods, ShoppingDispatchType, ShoppingStateContextValue, useShoppingDispatch, useShoppingState } from '../../context'
|
||||||
import { ShoppingStore } from '../../context/shoppingStore'
|
import { ShoppingStore } from '../../context/shoppingStore'
|
||||||
|
import LabAndImg from '@/components/LabAndImg'
|
||||||
|
|
||||||
type PropsType = {
|
type PropsType = {
|
||||||
state?: {
|
state?: {
|
||||||
@ -89,7 +90,9 @@ let ColorKindItem: FC<PropsType> = props => {
|
|||||||
state?.Observer?.notify(purchaserId)
|
state?.Observer?.notify(purchaserId)
|
||||||
}
|
}
|
||||||
}, 400)
|
}, 400)
|
||||||
|
const labAndImgObj = useCallback(item => {
|
||||||
|
return { lab: item.lab, rgb: item.rgb, texture_url: item.product_color_texture_url }
|
||||||
|
}, [])
|
||||||
return (
|
return (
|
||||||
<MCheckbox
|
<MCheckbox
|
||||||
status={state?.multipleSelection?.hasOwnProperty(itemData.id) || false}
|
status={state?.multipleSelection?.hasOwnProperty(itemData.id) || false}
|
||||||
@ -98,7 +101,9 @@ let ColorKindItem: FC<PropsType> = props => {
|
|||||||
customClassName={classnames(styles.checkbox, state?.multipleSelection?.hasOwnProperty(itemData.id) ? styles.selected : '')}
|
customClassName={classnames(styles.checkbox, state?.multipleSelection?.hasOwnProperty(itemData.id) ? styles.selected : '')}
|
||||||
customTextClass={styles.colorKindItem}>
|
customTextClass={styles.colorKindItem}>
|
||||||
<View className={styles['colorKindItem__left']}>
|
<View className={styles['colorKindItem__left']}>
|
||||||
<Image className={styles['colorKindItem__left--image']} mode='aspectFill' src={formatImgUrl(itemData.product_color_texture_url)}></Image>
|
<LabAndImg value={labAndImgObj(itemData)} />
|
||||||
|
|
||||||
|
{/* <Image className={styles['colorKindItem__left--image']} mode='aspectFill' src={formatImgUrl(itemData.product_color_texture_url)}></Image> */}
|
||||||
</View>
|
</View>
|
||||||
<View className={styles['colorKindItem__center']}>
|
<View className={styles['colorKindItem__center']}>
|
||||||
<Text className={styles['colorKindItem__center--title']}>
|
<Text className={styles['colorKindItem__center--title']}>
|
||||||
|
@ -16,6 +16,7 @@ import classNames from 'classnames'
|
|||||||
import LoadingCard from '@/components/loadingCard'
|
import LoadingCard from '@/components/loadingCard'
|
||||||
import { ShoppingCartListApi } from '@/api'
|
import { ShoppingCartListApi } from '@/api'
|
||||||
import { ShoppingStore } from '../../context/shoppingStore'
|
import { ShoppingStore } from '../../context/shoppingStore'
|
||||||
|
import { usePropsValue } from '@/use/useCommon'
|
||||||
|
|
||||||
interface ButtonPropsType {
|
interface ButtonPropsType {
|
||||||
isActive: boolean
|
isActive: boolean
|
||||||
@ -61,7 +62,12 @@ type PropsType = {
|
|||||||
|
|
||||||
let ShoppingCartItem: FC<PropsType> = props => {
|
let ShoppingCartItem: FC<PropsType> = props => {
|
||||||
const { state } = props
|
const { state } = props
|
||||||
const [itemData, setItemData] = useState(props.itemData)
|
console.log('props ShoppingCartItem', props)
|
||||||
|
|
||||||
|
const [itemData, setItemData] = usePropsValue({
|
||||||
|
value: props.itemData,
|
||||||
|
defaultValue: props.itemData,
|
||||||
|
})
|
||||||
const { multipleSelection, currentCheckedPurchaserId } = state!
|
const { multipleSelection, currentCheckedPurchaserId } = state!
|
||||||
const dispatch = useShoppingDispatch()
|
const dispatch = useShoppingDispatch()
|
||||||
|
|
||||||
@ -184,7 +190,7 @@ let ShoppingCartItem: FC<PropsType> = props => {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
setItemData(res.data[0])
|
setItemData(() => res.data[0])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// 取消订阅
|
// 取消订阅
|
||||||
@ -276,7 +282,7 @@ interface GoodsListPropType {
|
|||||||
startTransition: React.TransitionStartFunction
|
startTransition: React.TransitionStartFunction
|
||||||
}
|
}
|
||||||
const GoodsList = memo<GoodsListPropType>(props => {
|
const GoodsList = memo<GoodsListPropType>(props => {
|
||||||
console.log('Rerender component: GoodsList', props.multipleSelection)
|
console.log('Rerender component: GoodsList', props)
|
||||||
const { itemData, selected, isPending, startTransition, multipleSelection } = props
|
const { itemData, selected, isPending, startTransition, multipleSelection } = props
|
||||||
|
|
||||||
const currentSelected = useRef<EnumSaleMode | null>(null)
|
const currentSelected = useRef<EnumSaleMode | null>(null)
|
||||||
@ -355,6 +361,10 @@ const GoodsList = memo<GoodsListPropType>(props => {
|
|||||||
const withStateSlice = (comp, slice) => {
|
const withStateSlice = (comp, slice) => {
|
||||||
const MemoComp = memo(comp, (prevProps, nextProps) => {
|
const MemoComp = memo(comp, (prevProps, nextProps) => {
|
||||||
let needMemo = true
|
let needMemo = true
|
||||||
|
console.log('---------------props-----------------------')
|
||||||
|
console.log('props prevProps==>', prevProps.itemData)
|
||||||
|
console.log('props nextProps==>', nextProps.itemData)
|
||||||
|
console.log('-----------------props---------------------')
|
||||||
if (JSON.stringify(prevProps.itemData) !== JSON.stringify(nextProps.itemData)) {
|
if (JSON.stringify(prevProps.itemData) !== JSON.stringify(nextProps.itemData)) {
|
||||||
needMemo = false
|
needMemo = false
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import { dataLoadingStatus, debounce, getFilterData } from '@/common/util'
|
|||||||
import { ShoppingProvider } from './components/shoppingCart/index'
|
import { ShoppingProvider } from './components/shoppingCart/index'
|
||||||
import { Goods, ShoppingDispatchType, useShoppingDispatch, useShoppingState } from './context'
|
import { Goods, ShoppingDispatchType, useShoppingDispatch, useShoppingState } from './context'
|
||||||
import { alert, goLink, isEmptyObject } from '@/common/common'
|
import { alert, goLink, isEmptyObject } from '@/common/common'
|
||||||
|
import LoadingCard from '@/components/loadingCard'
|
||||||
|
|
||||||
export const Shopping: FC = memo(() => {
|
export const Shopping: FC = memo(() => {
|
||||||
// 计算总的预估金额
|
// 计算总的预估金额
|
||||||
@ -122,10 +123,11 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
|
|||||||
})()
|
})()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (state.success) {
|
if (state.success) {
|
||||||
setShoppingCartData({ list: state.data, total: state.data.length })
|
// startTransition(() => {
|
||||||
|
setShoppingCartData({ list: state.data, total: state.data.length })
|
||||||
|
// })
|
||||||
}
|
}
|
||||||
}, [state])
|
}, [state])
|
||||||
|
|
||||||
@ -165,6 +167,7 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
|
|||||||
if (res.success) {
|
if (res.success) {
|
||||||
alert.success('删除成功')
|
alert.success('删除成功')
|
||||||
fetchData(searchOptions)
|
fetchData(searchOptions)
|
||||||
|
// Observer.notify(currentCheckedPurchaserId)
|
||||||
} else {
|
} else {
|
||||||
alert.none(res.msg)
|
alert.none(res.msg)
|
||||||
}
|
}
|
||||||
@ -207,6 +210,7 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
|
|||||||
setRefreshStatus(false)
|
setRefreshStatus(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// const [isPending, startTransition] = useTransition()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View className={classnames('flex-col', styles.shopping)} id='shoppingContainer'>
|
<View className={classnames('flex-col', styles.shopping)} id='shoppingContainer'>
|
||||||
@ -224,10 +228,16 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
|
|||||||
<View className={classnames('flex-item', 'flex-col', styles['shopping--context'])}>
|
<View className={classnames('flex-item', 'flex-col', styles['shopping--context'])}>
|
||||||
<View id='shoppingListContainer' className={classnames(styles.shopping__list__container, 'flex-item')} style={{ height: listHeightRef.current }}>
|
<View id='shoppingListContainer' className={classnames(styles.shopping__list__container, 'flex-item')} style={{ height: listHeightRef.current }}>
|
||||||
<InfiniteScroll statusMore={statusMore} refresherEnabled={true} selfOnRefresherRefresh={handleRefresh} refresherTriggered={refreshStatus}>
|
<InfiniteScroll statusMore={statusMore} refresherEnabled={true} selfOnRefresherRefresh={handleRefresh} refresherTriggered={refreshStatus}>
|
||||||
{!!shoppingCartData?.list?.length &&
|
{
|
||||||
|
// isPending ? (
|
||||||
|
// null
|
||||||
|
// ) : (
|
||||||
|
!!shoppingCartData?.list?.length &&
|
||||||
shoppingCartData?.list?.map((item, index) => {
|
shoppingCartData?.list?.map((item, index) => {
|
||||||
return <ItemList itemData={item} key={index}></ItemList>
|
return <ItemList itemData={item} key={index}></ItemList>
|
||||||
})}
|
})
|
||||||
|
// )
|
||||||
|
}
|
||||||
</InfiniteScroll>
|
</InfiniteScroll>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
@ -167,7 +167,7 @@ export function usePropsValue<T>(options: UsePropsValueOptions<T>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const setState = useCallback((v: SetStateAction<T>, forceTrigger: boolean = false) => {
|
const setState = useCallback((v: SetStateAction<T>, forceTrigger: boolean = false) => {
|
||||||
// `forceTrigger` means trigger `onChange` even if `v` is the same as `stateRef.current`
|
// `forceTrigger` 意思是 触发 `onChange` 不管 `v` 是否和 `stateRef.current` 相等
|
||||||
const nextValue = typeof v === 'function' ? (v as (prevState: T) => T)(stateRef.current) : v
|
const nextValue = typeof v === 'function' ? (v as (prevState: T) => T)(stateRef.current) : v
|
||||||
if (!forceTrigger && nextValue === stateRef.current) return
|
if (!forceTrigger && nextValue === stateRef.current) return
|
||||||
stateRef.current = nextValue
|
stateRef.current = nextValue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user