Skip to content

Commit

Permalink
Debug - hook to understand component lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Oct 29, 2023
1 parent a577823 commit 7e86104
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/common/components/useDebugHook.ts
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 };
};

1 comment on commit 7e86104

@vercel
Copy link

@vercel vercel bot commented on 7e86104 Oct 29, 2023

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

Please sign in to comment.