From 6427844da3877f531baf66a112433a9fde32f518 Mon Sep 17 00:00:00 2001 From: djmil Date: Fri, 10 Nov 2023 13:17:38 +0100 Subject: [PATCH] usePrevious hook --- webapp/src/hook/Previous.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 webapp/src/hook/Previous.js diff --git a/webapp/src/hook/Previous.js b/webapp/src/hook/Previous.js new file mode 100644 index 0000000..d3e03ca --- /dev/null +++ b/webapp/src/hook/Previous.js @@ -0,0 +1,24 @@ +import { useRef, useEffect } from 'react'; + +export default function usePrevious(value) { + const ref = useRef(); + + useEffect(() => { + ref.current = value; + }); + + return ref.current; +} + +/* Usage example + +const useLocationChange = (action) => { + const location = useLocation(); + const prevLocation = usePrevious(location); + + useEffect(() => { + action(location, prevLocation); + }, [location]); +} + +*/ \ No newline at end of file