From 43d619df47a0ed6e393f1d18a2c92d93402496ef Mon Sep 17 00:00:00 2001 From: "Dmytro ryliov.work@gmail.com Rylov" Date: Thu, 13 Feb 2025 12:54:13 +0100 Subject: [PATCH 1/2] fix: widget isn't destroyed on unmount --- .../src/components/CldUploadWidget/CldUploadWidget.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx index 8d2e98f7..257c533c 100644 --- a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx +++ b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx @@ -137,7 +137,7 @@ const CldUploadWidget = ({ widget.current?.destroy(); widget.current = undefined; } - }, []) + }, [widget.current]) /** * Instance Methods @@ -248,4 +248,4 @@ const CldUploadWidget = ({ ); }; -export default CldUploadWidget; \ No newline at end of file +export default CldUploadWidget; From ced9949618a863c0963c13318eeb53ba05c6296b Mon Sep 17 00:00:00 2001 From: "Dmytro ryliov.work@gmail.com Rylov" Date: Fri, 14 Feb 2025 01:35:56 +0100 Subject: [PATCH 2/2] update: preventing handleOnLoad execution if component unmounted --- .../components/CldUploadWidget/CldUploadWidget.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx index 257c533c..7fd79b26 100644 --- a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx +++ b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx @@ -36,6 +36,7 @@ const CldUploadWidget = ({ }: CldUploadWidgetProps) => { const cloudinary: CldUploadWidgetCloudinaryInstance = useRef(); const widget: CldUploadWidgetWidgetInstance = useRef(); + const isMounted = useRef(); const [error, setError] = useState(undefined); const [results, setResults] = useState(undefined); @@ -116,6 +117,10 @@ const CldUploadWidget = ({ */ function handleOnLoad() { + if (!isMounted.current) { + return; + } + setIsScriptLoading(false); if ( !cloudinary.current ) { @@ -132,12 +137,17 @@ const CldUploadWidget = ({ }); } + useEffect(() => { + isMounted.current = true; + },[]) + useEffect(() => { return () => { + isMounted.current = false; widget.current?.destroy(); widget.current = undefined; } - }, [widget.current]) + }, []) /** * Instance Methods