-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debug - hook to understand component lifetimes
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as React from 'react'; | ||
|
||
let instanceCounter = 0; | ||
|
||
function getRandom1000() { | ||
const number = Math.round(Math.random() * 1000); | ||
console.log('~random', number); | ||
return number; | ||
} | ||
|
||
function increment() { | ||
const number = instanceCounter++; | ||
console.log('+increment', number); | ||
return number; | ||
} | ||
|
||
export const useDebugHook = (app: string) => { | ||
// test behavior of React.useState - note the function syntax, so we don't call the initializer on every render | ||
const [test, setTest] = React.useState<number>(() => getRandom1000()); | ||
// test behavior of React.useRef with instance counter | ||
const testRef = React.useRef(() => increment()); | ||
|
||
console.log(app, 'render', test, testRef.current); | ||
|
||
React.useEffect(() => { | ||
console.log(app, 'effect', test, testRef.current); | ||
return () => { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
console.log(app, 'cleanup', test, testRef?.current); | ||
}; | ||
}, [app, test]); | ||
|
||
return { test, setTest }; | ||
}; |
7e86104
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
big-agi – ./
get.big-agi.com
big-agi-git-main-enricoros.vercel.app
big-agi-enricoros.vercel.app