diff --git a/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx b/next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx index 8d2e98f7..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,8 +137,13 @@ const CldUploadWidget = ({ }); } + useEffect(() => { + isMounted.current = true; + },[]) + useEffect(() => { return () => { + isMounted.current = false; widget.current?.destroy(); widget.current = undefined; } @@ -248,4 +258,4 @@ const CldUploadWidget = ({ ); }; -export default CldUploadWidget; \ No newline at end of file +export default CldUploadWidget;