Skip to content

Commit

Permalink
chore: cleanup custom hooks
Browse files Browse the repository at this point in the history
Related to hto#444
  • Loading branch information
Skaiir committed Jan 9, 2024
1 parent 85361a6 commit 704b5b7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/form-js-editor/src/render/hooks/usePrevious.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
} from 'preact/hooks';


export default function usePrevious(value) {
const ref = useRef();
export default function usePrevious(value, defaultValue = null) {
const ref = useRef(defaultValue);

useEffect(() => ref.current = value);
useEffect(() => ref.current = value, [ value ]);

return ref.current;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function Textarea(props) {
field,
value: target.value
});
}, [ props.onChange ]);
});

const onInputBlur = () => {
flushOnChange && flushOnChange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function Textfield(props) {
field,
value: target.value
});
}, [ props.onChange ]);
});

const onInputBlur = () => {
flushOnChange && flushOnChange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function useDeepCompareState(value, defaultValue) {

const [ state, setState ] = useState(defaultValue);

const previous = usePrevious(value, defaultValue, [ value ]);
const previous = usePrevious(value, defaultValue);

const changed = !isEqual(previous, value);

Expand Down
6 changes: 3 additions & 3 deletions packages/form-js-viewer/src/render/hooks/useFlushDebounce.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useRef } from 'preact/hooks';
import useService from './useService';

function useFlushDebounce(func, additionalDeps = []) {
function useFlushDebounce(func) {

const timeoutRef = useRef(null);
const lastArgsRef = useRef(null);
Expand Down Expand Up @@ -29,7 +29,7 @@ function useFlushDebounce(func, additionalDeps = []) {
lastArgsRef.current = null;
}, delay);

}, [ func, delay, shouldDebounce, ...additionalDeps ]);
}, [ func, delay, shouldDebounce ]);

const flushFunc = useCallback(() => {

Expand All @@ -42,7 +42,7 @@ function useFlushDebounce(func, additionalDeps = []) {
timeoutRef.current = null;
}

}, [ func, ...additionalDeps ]);
}, [ func ]);

return [ debounceFunc, flushFunc ];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/form-js-viewer/src/render/hooks/usePrevious.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
} from 'preact/hooks';


export default function usePrevious(value, defaultValue, dependencies) {
export default function usePrevious(value, defaultValue = null) {
const ref = useRef(defaultValue);

useEffect(() => ref.current = value, dependencies);
useEffect(() => ref.current = value, [ value ]);

return ref.current;
}
8 changes: 4 additions & 4 deletions packages/form-js-viewer/test/helper/preactDebuggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const usePrevious = (value, initialValue) => {
const ref = useRef(initialValue);
useEffect(() => {
ref.current = value;
});
}, [ value ]);
return ref.current;
};

export function useEffectDebugger(effectHook, dependencies, dependencyNames = [], effectName = 'noname') {
export function useEffectDebugger(effect, dependencies, dependencyNames = [], effectName = 'noname') {
const previousDeps = usePrevious(dependencies, []);

const changedDeps = dependencies.reduce((accum, dependency, index) => {
Expand All @@ -30,7 +30,7 @@ export function useEffectDebugger(effectHook, dependencies, dependencyNames = []
console.log('[use-effect-debugger] (' + effectName + ') ', changedDeps);
}

useEffect(effectHook, dependencies);
useEffect(effect, [ effect, ...dependencies ]);
}

export function useCallbackDebugger(callback, dependencies, dependencyNames = [], callbackName = 'noname') {
Expand All @@ -55,5 +55,5 @@ export function useCallbackDebugger(callback, dependencies, dependencyNames = []
console.log('[use-callback-debugger] (' + callbackName + ') ', changedDeps);
}

return useCallback(callback, dependencies);
return useCallback(callback, [ callback, ...dependencies ]);
}

0 comments on commit 704b5b7

Please sign in to comment.