Skip to content

Instantly share code, notes, and snippets.

@dextermb
Created January 17, 2020 10:21
Show Gist options
  • Save dextermb/1af9e6dc72c21cb422c911b03f3beab6 to your computer and use it in GitHub Desktop.
Save dextermb/1af9e6dc72c21cb422c911b03f3beab6 to your computer and use it in GitHub Desktop.
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