Skip to content

Instantly share code, notes, and snippets.

@chanonroy
Created February 18, 2021 14:16
Show Gist options
  • Select an option

  • Save chanonroy/2e169c185e09072898e5241695da02d9 to your computer and use it in GitHub Desktop.

Select an option

Save chanonroy/2e169c185e09072898e5241695da02d9 to your computer and use it in GitHub Desktop.
import { debounce } from 'lodash'
import { track } from './analytics-service'
export default function App() {
const [value, setValue] = useState("")
const trackAnalytics = (val) => {
if (val) track(val)
}
// This won't work. It will be triggered after each keystroke
const debouncedTrack = debounce(trackAnalytics, 500)
const handleChange = (val) => {
setValue(val)
debouncedTrack(val)
}
return (
<div>
<div>{value}</div>
<input value={value} onChange={e => handleChange(e.target.value)} />
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment