Created
December 20, 2016 00:53
-
-
Save brunolemos/df7621394abbfabfffc23f2b455e960d to your computer and use it in GitHub Desktop.
React Debounce Render
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
// usage: | |
// export default debounce(100)(MyComponent); | |
import React from 'react'; | |
import debounce from 'lodash/debounce'; | |
export default (interval, ...debounceArgs) => { | |
if (typeof interval !== 'number' && interval > 0) { | |
throw new Error('[debounce] Interval (ms) parameter not received.'); | |
} | |
return Component => class extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
this.state = props; | |
} | |
updateStateWithProps = (props) => this.setState(props || this.props); | |
componentWillReceiveProps = debounce(this.updateStateWithProps, interval, ...debounceArgs); | |
render = () => <Component {...this.state} />; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment