Skip to content

Instantly share code, notes, and snippets.

@igmoweb
Last active October 2, 2020 09:27
Show Gist options
  • Save igmoweb/4a39891fede51a850428704d82ba4a7d to your computer and use it in GitHub Desktop.
Save igmoweb/4a39891fede51a850428704d82ba4a7d to your computer and use it in GitHub Desktop.
// Always run
useEffect( () => {
// code to run
} );
// As componentDidMount (run once)
useEffect( () => {
// code to run
}, [] );
// Run only when attr changes
useEffect( () => {
// code to run
}, [ attr ] );
// Run only when component updates (componentDidUpdate)
const didMountRef = useRef( false );
useEffect( () => {
if (didMountRef.current) {
// code to run
} else didMountRef.current = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment