import React, { memo, useCallback, useMemo } from 'react' import { Pressable, StyleSheet, Text, View } from 'react-native' import { Ionicons } from '@expo/vector-icons' export interface FavoriteButtonProps { favorited?: boolean loading?: boolean count?: number size?: number onPress?: () => void testID?: string } const FavoriteButtonComponent: React.FC = ({ favorited = false, loading = false, count, size = 24, onPress, testID, }) => { // 使用 useCallback 缓存 onPress 回调 const handlePress = useCallback(() => { if (!loading && onPress) { onPress() } }, [loading, onPress]) // 缓存样式计算 - 移除不必要的 iconStyle const containerStyle = useMemo( () => [styles.container, loading && styles.disabled], [loading] ) // 根据收藏状态选择图标名称和颜色 const iconName = favorited ? 'star' : 'star-outline' const iconColor = favorited ? '#FFD700' : '#8E8E93' return ( {count !== undefined && ( {count} )} ) } export const FavoriteButton = memo(FavoriteButtonComponent) const styles = StyleSheet.create({ container: { flexDirection: 'row', alignItems: 'center', padding: 8, }, disabled: { opacity: 0.5, }, content: { flexDirection: 'row', alignItems: 'center', gap: 4, }, count: { color: '#8E8E93', fontWeight: '600', marginLeft: 4, }, })