From d75b7a75e0a808b02f9e28dc91547ac9e11e13b6 Mon Sep 17 00:00:00 2001 From: Valentin Serra Date: Thu, 11 Jan 2024 13:03:19 +0100 Subject: [PATCH] chore: fix internal usePrevious hooks Related to https://github.com/bpmn-io/form-js/pull/972#discussion_r1448611855 --- .../form-js-editor/src/render/hooks/usePrevious.js | 14 ++++++++------ .../form-js-viewer/src/render/hooks/usePrevious.js | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/form-js-editor/src/render/hooks/usePrevious.js b/packages/form-js-editor/src/render/hooks/usePrevious.js index a28114be7..9f8d11796 100644 --- a/packages/form-js-editor/src/render/hooks/usePrevious.js +++ b/packages/form-js-editor/src/render/hooks/usePrevious.js @@ -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; } \ No newline at end of file diff --git a/packages/form-js-viewer/src/render/hooks/usePrevious.js b/packages/form-js-viewer/src/render/hooks/usePrevious.js index a28114be7..9f8d11796 100644 --- a/packages/form-js-viewer/src/render/hooks/usePrevious.js +++ b/packages/form-js-viewer/src/render/hooks/usePrevious.js @@ -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; } \ No newline at end of file