mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
28 lines
700 B
TypeScript
28 lines
700 B
TypeScript
|
import usePrevious from './usePrevious';
|
||
|
import { useEffect } from 'react';
|
||
|
|
||
|
export default function useEffectDebugger(effectHook:any, dependencies:any, dependencyNames:any[] = []) {
|
||
|
const previousDeps = usePrevious(dependencies, []);
|
||
|
|
||
|
const changedDeps = dependencies.reduce((accum:any, dependency:any, index:any) => {
|
||
|
if (dependency !== previousDeps[index]) {
|
||
|
const keyName = dependencyNames[index] || index;
|
||
|
return {
|
||
|
...accum,
|
||
|
[keyName]: {
|
||
|
before: previousDeps[index],
|
||
|
after: dependency,
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
return accum;
|
||
|
}, {});
|
||
|
|
||
|
if (Object.keys(changedDeps).length) {
|
||
|
console.log('[use-effet-debugger] ', changedDeps);
|
||
|
}
|
||
|
|
||
|
useEffect(effectHook, dependencies);
|
||
|
}
|