Skip to content

Instantly share code, notes, and snippets.

@Pinwheeler
Created March 29, 2022 17:46
Show Gist options
  • Save Pinwheeler/baa3267cf99e5d67c1e1d76497b45bf7 to your computer and use it in GitHub Desktop.
Save Pinwheeler/baa3267cf99e5d67c1e1d76497b45bf7 to your computer and use it in GitHub Desktop.
UseDebug
// COPYRIGHT 2020 MIT Open Source - Anthony Dreessen
// This file is basically a slightly modified StackOverflow answer
// and should be owned by nobody- honestly it should be a part of React itself
// full license at the bottom
import { useEffect, useMemo, useRef } from "react";
export const usePrevious = (value: any, initialValue: any) => {
const ref = useRef(initialValue);
useEffect(() => {
ref.current = value;
});
return ref.current;
};
/**
* usage: replace `useEffect` with `useEffectDebugger`. You may optionally provide the names of the dependencies
*
* `useEffect(() => {}, [dep])`
* `useEffectDebugger(() => {}, [dep], ["dep"])`
*/
export const useEffectDebugger = (
effectHook: React.EffectCallback,
dependencies: React.DependencyList,
dependencyNames: string[] = []
) => {
const previousDeps = usePrevious(dependencies, []);
const changedDeps = dependencies.reduce((accum, dependency, index) => {
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-effect-debugger] ", changedDeps);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(effectHook, dependencies);
};
/**
* usage: replace `useMemo` with `useMemoDebugger`. You may optionally provide the names of the dependencies
*
* `useMemo(() => {}, [dep])`
* `useMemo(() => {}, [dep], ["dep"])`
*/
export const useMemoDebugger = <T>(
factory: () => T,
dependencies: React.DependencyList,
dependencyNames: string[] = []
) => {
const previousDeps = usePrevious(dependencies, []);
const changedDeps = dependencies.reduce((accum, dependency, index) => {
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-memo-debugger] ", changedDeps);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(factory, dependencies);
};
// Copyright 2021 Anthony Dreessen
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment