Created
January 7, 2024 10:37
-
-
Save GalindoSVQ/d469a0d256ca775f5f412176a7f7d276 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
| React.useEffectEvent = React.experimental_useEffectEvent; | |
| export function useLogger(name, ...rest) { | |
| const initialRenderRef = React.useRef(true); | |
| const handleLog = React.useEffectEvent((event) => { | |
| console.log(`${name} ${event}:`, rest); | |
| }); | |
| React.useEffect(() => { | |
| if (initialRenderRef.current === false) { | |
| handleLog('updated'); | |
| } | |
| }); | |
| React.useEffect(() => { | |
| handleLog('mounted'); | |
| initialRenderRef.current = false; | |
| return () => { | |
| handleLog('unmounted'); | |
| initialRenderRef.current = true; | |
| }; | |
| }, []); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackblitz.com/edit/stackblitz-starters-l54ygb