Passed
Push — feature/improve-applicant-list ( 3fd796...cbd310 )
by Chris
04:06
created

resources/assets/js/hooks/useDependenciesDebugger.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 27
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 22
c 0
b 0
f 0
dl 0
loc 27
rs 10
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import { useRef, useMemo } from "react";
2
3
const compareInputs = (inputKeys, oldInputs, newInputs) => {
4
  inputKeys.forEach(key => {
5
    const oldInput = oldInputs[key];
6
    const newInput = newInputs[key];
7
    if (oldInput !== newInput) {
8
      console.log("change detected", key, "old:", oldInput, "new:", newInput);
9
    }
10
  });
11
};
12
13
const useDependenciesDebugger = inputs => {
14
  const oldInputsRef = useRef(inputs);
15
  const inputValuesArray = Object.values(inputs);
16
  const inputKeysArray = Object.keys(inputs);
17
  useMemo(() => {
18
    const oldInputs = oldInputsRef.current;
19
20
    compareInputs(inputKeysArray, oldInputs, inputs);
21
22
    oldInputsRef.current = inputs;
23
  }, inputValuesArray); // eslint-disable-line react-hooks/exhaustive-deps
24
};
25
26
export default useDependenciesDebugger;
27