Skip to content

Instantly share code, notes, and snippets.

@wachunei
Created January 12, 2018 15:27
Show Gist options
  • Save wachunei/96d697aa80d3c6b9992b54505dd5a9f9 to your computer and use it in GitHub Desktop.
Save wachunei/96d697aa80d3c6b9992b54505dd5a9f9 to your computer and use it in GitHub Desktop.
withDebounce HOC
import React from 'react';
import PropTypes from 'prop-types';
import debounce from 'lodash/debounce';
const withDebounce = (Touchable) => {
class DebouncedComponent extends React.PureComponent {
constructor(props) {
super(props);
const { onPress } = props;
this.onPress = debounce(() => onPress && onPress(), 300, {
leading: true,
trailing: false,
});
}
render() {
return <Touchable {...this.props} onPress={this.onPress} />;
}
}
DebouncedComponent.defaultProps = {
onPress: null,
};
DebouncedComponent.propTypes = {
onPress: PropTypes.func,
};
return DebouncedComponent;
};
export default withDebounce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment