Skip to content

Instantly share code, notes, and snippets.

@simbathesailor
Last active January 8, 2020 15:07
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/1d00a0ac510b3577b9c6e5eb6c458569 to your computer and use it in GitHub Desktop.
Save simbathesailor/1d00a0ac510b3577b9c6e5eb6c458569 to your computer and use it in GitHub Desktop.
debouncedEffect3.jsx
function ChildComponent({ value, setValue }) {
function makeApiCallRaw(value) {
console.log("make api call with value", value);
}
const debouncedmakeApiCall = React.useCallback(
debounce(
value => {
makeApiCallRaw(value);
},
2000,
{ trailing: true }
),
[]
);
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