import React, { useCallback, useRef, useState } from "react"; import { StyleSheet, TouchableOpacity, TouchableOpacityProps, ActivityIndicator } from "react-native"; const DEFAULT_BORDER_RADIUS = 14; interface Props { loadingComponent?: React.ReactNode; disabled?: boolean; onPress: () => Promise; labelComponent: React.ReactNode; style?: TouchableOpacityProps['style']; } const ThrottleButton = ({ loadingComponent = , disabled = false, labelComponent, onPress, style = {}, }: Props) => { const [isLoading, setIsLoading] = useState(false); const isThrottle = useRef(false); const handlePress = useCallback(async () => { if (isThrottle.current) return; isThrottle.current = true; setIsLoading(true); try { await onPress(); } finally { setIsLoading(false); isThrottle.current = false; } }, [onPress]); return ( { isLoading ? loadingComponent : labelComponent } ) } const styles = StyleSheet.create({ container: { justifyContent: 'center', alignItems: 'center', borderRadius: DEFAULT_BORDER_RADIUS, overflow: 'hidden', flexDirection: 'row' }, }); export default ThrottleButton;