Skip to content

Instantly share code, notes, and snippets.

@devmrin
Last active June 4, 2019 20:22
Show Gist options
  • Save devmrin/be63ef3e6128ead558dc1eea17fb11b1 to your computer and use it in GitHub Desktop.
Save devmrin/be63ef3e6128ead558dc1eea17fb11b1 to your computer and use it in GitHub Desktop.
Touchable Debounce - Medium Article
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