Skip to content

Instantly share code, notes, and snippets.

@felipefunes
Created March 5, 2019 21:11
Show Gist options
  • Save felipefunes/11a42da3d996d20edc53a4a66b5b5ce9 to your computer and use it in GitHub Desktop.
Save felipefunes/11a42da3d996d20edc53a4a66b5b5ce9 to your computer and use it in GitHub Desktop.
// GOOD! Place the condition inside the Hook
useEffect(() => {
if (posts === null) {
// This will run only if posts are not still fetched.
// If you mount this component more than once,
// the request won't beexecuted again
getPosts();
}
}, []); // Besides, with the empty array this Effect just run once
// BAD! Wrapping the Effect insithe the condition
if (posts === null) {
useEffect(() => {
getPosts();
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment