-
-
Save getify/13d2138c7ba61bdca67c9d0014420efe to your computer and use it in GitHub Desktop.
useEffect + CAF (ref: https://overreacted.io/a-complete-guide-to-useeffect/)
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
function Article({ id }) { | |
const [article, setArticle] = useState(null); | |
useEffect(() => { | |
let cancelToken = new CAF.cancelToken(); | |
let fetchData = CAF(function *fetchData(signal) { | |
const article = yield API.fetchArticle(id); | |
setArticle(article); | |
}); | |
fetchData(cancelToken); | |
return () => { | |
cancelToken.abort(); | |
}; | |
}, [id]); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment