Skip to content

Commit

Permalink
chore: fix internal usePrevious hooks
Browse files Browse the repository at this point in the history
Related to #972 (comment)
  • Loading branch information
Skaiir committed Jan 11, 2024
1 parent 0f043bd commit d75b7a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions packages/form-js-editor/src/render/hooks/usePrevious.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
useEffect,
useRef
useState
} from 'preact/hooks';


export function usePrevious(value, defaultValue = null) {
const ref = useRef(defaultValue);
const [ current, setCurrent ] = useState(value);
const [ previous, setPrevious ] = useState(defaultValue);

useEffect(() => ref.current = value, [ value ]);
if (value !== current) {
setPrevious(current);
setCurrent(value);
}

return ref.current;
return previous;
}
14 changes: 8 additions & 6 deletions packages/form-js-viewer/src/render/hooks/usePrevious.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
useEffect,
useRef
useState
} from 'preact/hooks';


export function usePrevious(value, defaultValue = null) {
const ref = useRef(defaultValue);
const [ current, setCurrent ] = useState(value);
const [ previous, setPrevious ] = useState(defaultValue);

useEffect(() => ref.current = value, [ value ]);
if (value !== current) {
setPrevious(current);
setCurrent(value);
}

return ref.current;
return previous;
}

0 comments on commit d75b7a7

Please sign in to comment.