Created
February 18, 2021 14:16
-
-
Save chanonroy/2e169c185e09072898e5241695da02d9 to your computer and use it in GitHub Desktop.
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 { 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