Created
December 11, 2020 08:26
-
-
Save 0ex-d/8123dc74782cd399c61eadabbe57b31a to your computer and use it in GitHub Desktop.
useEffect vs useLayoutEffect in one simple component.
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
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