Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@simbathesailor
Last active January 8, 2020 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simbathesailor/fe73dc6cefd6f59d50730bd65e9bacfa to your computer and use it in GitHub Desktop.
Save simbathesailor/fe73dc6cefd6f59d50730bd65e9bacfa to your computer and use it in GitHub Desktop.
debouncedEffectSnippet2.jsx
import debounce from "lodash/debounce";
function makeApiCallRaw(value) {
console.log("make api call with value", value);
}
const debouncedmakeApiCall = debounce(makeApiCallRaw, 200, { trailing: true });
function ChildComponent({ value, setValue }) {
return (
<input
defaultValue={value}
value={value}
onChange={e => {
setValue(e.target.value); // Has to be non debounced
debouncedmakeApiCall(e.target.value); // has to be debounced
}}
/>
);
}
function App() {
const [value, setValue] = React.useState("");
return (
<div>
Hello World
<ChildComponent value={value} setValue={setValue} />
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment