diff --git a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx index 9e5f55a7..0aaffe19 100644 --- a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx +++ b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx @@ -50,6 +50,7 @@ const CldUploadWidget = ({ const uploadWidgetId = useUploadWidgetId(); const cloudinary: CldUploadWidgetCloudinaryInstance = useRef(); const widget: CldUploadWidgetWidgetInstance = useRef(); + const isMounted = useRef(); const [error, setError] = useState(undefined); const [results, setResults] = useState(undefined); @@ -130,6 +131,10 @@ const CldUploadWidget = ({ */ function handleOnLoad() { + if (!isMounted.current) { + return; + } + setIsScriptLoading(false); if ( !cloudinary.current ) { @@ -146,8 +151,13 @@ const CldUploadWidget = ({ }); } + useEffect(() => { + isMounted.current = true; + },[]) + useEffect(() => { return () => { + isMounted.current = false; widget.current?.destroy(); widget.current = undefined; } @@ -262,4 +272,4 @@ const CldUploadWidget = ({ ); }; -export default CldUploadWidget; \ No newline at end of file +export default CldUploadWidget;