Created
January 12, 2018 15:27
-
-
Save wachunei/96d697aa80d3c6b9992b54505dd5a9f9 to your computer and use it in GitHub Desktop.
withDebounce HOC
This file contains hidden or 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 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