Last active
July 23, 2018 06:29
-
-
Save taulutwashi/cb4077d459c0527a787a7cd150ee339c to your computer and use it in GitHub Desktop.
React Native- Prevent Double click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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