Last active
June 4, 2019 20:22
-
-
Save devmrin/be63ef3e6128ead558dc1eea17fb11b1 to your computer and use it in GitHub Desktop.
Touchable Debounce - Medium Article
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, { PureComponent } from "react"; | |
import { PropTypes, ViewPropTypes, TouchableOpacity } from "react-native"; | |
import { debounce } from "underscore"; | |
//PureComponent handles shouldComponentUpdate for you. | |
class TouchableDebounce extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
} | |
handlePress = () => { | |
const { onPress, debounceTime, handleFirstTap } = this.props; | |
debounce(onPress, debounceTime, handleFirstTap); | |
}; | |
render() { | |
return ( | |
<TouchableOpacity | |
{...this.props} | |
onPress={this.handlePress} | |
> | |
{this.props.children} | |
</TouchableOpacity> | |
); | |
} | |
} | |
TouchableDebounce.propTypes = { | |
onPress: PropTypes.func.isRequired, | |
style: ViewPropTypes.style, | |
handleFirstTap: PropTypes.bool, | |
debounceTime: PropTypes.number | |
}; | |
TouchableDebounce.defaultProps = { | |
style: {}, | |
handleFirstTap: true, | |
debounceTime: 500 | |
}; | |
export default TouchableDebounce; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment