Created
January 17, 2020 10:21
-
-
Save dextermb/1af9e6dc72c21cb422c911b03f3beab6 to your computer and use it in GitHub Desktop.
This file contains 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' | |
class Debounce extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
value: '' | |
} | |
this.timeout = null | |
this.onChange = this.onChange.bind(this) | |
this.onDebounce = this.onDebounce.bind(this) | |
} | |
onChange(event) { | |
const { target: { value } } = event | |
clearTimeout(this.timeout) | |
this.setState( | |
{ value }, | |
() => ( | |
this.timeout = ( | |
setTimeout(this.debounce, 500) | |
) | |
) | |
) | |
} | |
onDebounce() { | |
const { value } = this.state | |
const { onDebounce } = this.props | |
onDebounce(value) | |
} | |
render() { | |
const { value } = this.state | |
return ( | |
<input | |
value={value} | |
onChange={this.onChange} | |
/> | |
) | |
} | |
} | |
export default Debounce |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment