xref: /MusicFree/src/hooks/useLogRerender.ts (revision 91c8197380501f56579772c90297157d6f3c3aab)
1*91c81973S猫头猫import {useEffect, useRef} from 'react';
2*91c81973S猫头猫
3*91c81973S猫头猫export default function (msg?: string, deps: any[] = []) {
4*91c81973S猫头猫    const idRef = useRef<number>();
5*91c81973S猫头猫    useEffect(() => {
6*91c81973S猫头猫        idRef.current = Math.random();
7*91c81973S猫头猫        console.log('Mount', msg ?? '', idRef.current);
8*91c81973S猫头猫        return () => {
9*91c81973S猫头猫            console.log('Unmount', msg ?? '', idRef.current);
10*91c81973S猫头猫        };
11*91c81973S猫头猫    }, []);
12*91c81973S猫头猫
13*91c81973S猫头猫    useEffect(() => {
14*91c81973S猫头猫        if (deps?.length !== 0) {
15*91c81973S猫头猫            console.log('State Change', msg ?? '', idRef.current);
16*91c81973S猫头猫        }
17*91c81973S猫头猫    }, deps);
18*91c81973S猫头猫
19*91c81973S猫头猫    useEffect(() => {
20*91c81973S猫头猫        idRef.current && console.log('Rerender: ', msg ?? '', idRef.current);
21*91c81973S猫头猫    });
22*91c81973S猫头猫}
23