Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import debounce from 'lodash/debounce';
  4.  
  5. const withDebounce = (Touchable) => {
  6. class DebouncedComponent extends React.PureComponent {
  7. constructor(props) {
  8. super(props);
  9. const { onPress } = props;
  10. this.onPress = debounce(() => onPress && onPress(), 300, {
  11. leading: true,
  12. trailing: false,
  13. });
  14. }
  15.  
  16. render() {
  17. return <Touchable {...this.props} onPress={this.onPress} />;
  18. }
  19. }
  20.  
  21. DebouncedComponent.defaultProps = {
  22. onPress: null,
  23. };
  24.  
  25. DebouncedComponent.propTypes = {
  26. onPress: PropTypes.func,
  27. };
  28.  
  29. return DebouncedComponent;
  30. };
  31.  
  32. export default withDebounce;
Add Comment
Please, Sign In to add comment