Skip to content

Instantly share code, notes, and snippets.

@taulutwashi
Last active July 23, 2018 06:29
Show Gist options
  • Save taulutwashi/cb4077d459c0527a787a7cd150ee339c to your computer and use it in GitHub Desktop.
Save taulutwashi/cb4077d459c0527a787a7cd150ee339c to your computer and use it in GitHub Desktop.
React Native- Prevent Double click
import React from 'react';
import debounce from 'lodash.debounce'; // 4.0.8
const withPreventDoubleClick = (WrappedComponent) => {
class PreventDoubleClick extends React.PureComponent {
debouncedOnPress = () => {
this.props.onPress && this.props.onPress();
}
onPress = debounce(this.debouncedOnPress, 300, { leading: true, trailing: false });
render() {
return <WrappedComponent {...this.props} onPress={this.onPress} />;
}
}
PreventDoubleClick.displayName = `withPreventDoubleClick(${WrappedComponent.displayName ||WrappedComponent.name})`
return PreventDoubleClick;
}
export default withPreventDoubleClick;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment