When to use UseEffect

useEffect(callback[, dependencies]);
const [contents, setContents] = useState([])

useEffect(() => {
fetch(url)
.then(res => res.json())
.then(json => setContents(json))
}, [])
const [draft, updateDraft] = useState('')useEffect(() => {
fetch(url, {
method: 'POST',
body: draft,
})
})
useEffect(() => {
/* do something */
const cleanup = () => {
/* delete something */
}
return cleanup
})
useEffect(() => {
const id = setInterval(() => {
console.log("This is logged to console.");
}, 5000);
const cleanup = () => clearInterval(id)
return cleanup
}, []);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store