Skip to content

Instantly share code, notes, and snippets.

@smidhonza
Last active January 14, 2020 11:06
Show Gist options
  • Save smidhonza/b51ba5b5d5ea936fc17a068b3d3ce600 to your computer and use it in GitHub Desktop.
Save smidhonza/b51ba5b5d5ea936fc17a068b3d3ce600 to your computer and use it in GitHub Desktop.
import React from 'react';
const Demo = () => {
const [value, setValue] = React.useState();
const [data, setData] = React.useState();
React.useEffect(() => {
const fetchData = async () => {
if (value) { // dont call the fetch on initial load when value is empty
const result = await fetch('https://.....');
setData(result);
}
};
fetchData();
}, [value]); // React.useEffect is listening for changes in [] bracket, so when the value in here change, that async function is called
return (
<div>
<input defaultValue={value} onChange={(event) => setValue(event.target.value)} />
</div>
);
};
export default Demo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment