Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Created December 11, 2020 08:26
Show Gist options
  • Save 0ex-d/8123dc74782cd399c61eadabbe57b31a to your computer and use it in GitHub Desktop.
Save 0ex-d/8123dc74782cd399c61eadabbe57b31a to your computer and use it in GitHub Desktop.
useEffect vs useLayoutEffect in one simple component.
import * as React from 'react';
export default (()=> {
const ref = React.useRef();
/*
* useEffect hook
* Used when DOM changes are unobservable
*/
React.useEffect(() => {
ref.value = '2019';
});
/*
* useLayoutEffect hook
* runs synchronously immediately after
* React has performed all DOM mutations
*/
React.useLayoutEffect(() => {
console.log(ref.value) // this should log an old value because this runs first
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment