Last active
January 14, 2020 11:06
-
-
Save smidhonza/b51ba5b5d5ea936fc17a068b3d3ce600 to your computer and use it in GitHub Desktop.
This file contains 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 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