capture zapRef current fo cleanup in ZapThreadsWrapper

This commit is contained in:
austinkelsay 2025-04-20 14:41:27 -05:00
parent 70e31bfe91
commit 02caab317c
No known key found for this signature in database
GPG Key ID: 5A763922E5BA08EE

View File

@ -9,6 +9,9 @@ const ZapThreadsWrapper = ({ anchor, user, relays, disable, className, isAuthori
if (!isAuthorized) { if (!isAuthorized) {
return; return;
} }
// Store the current value of zapRef to use in the cleanup function
const currentZapRef = zapRef.current;
// Create a new <script> element // Create a new <script> element
const script = document.createElement('script'); const script = document.createElement('script');
@ -45,15 +48,15 @@ const ZapThreadsWrapper = ({ anchor, user, relays, disable, className, isAuthori
}); });
// Remove any existing <zap-threads> element // Remove any existing <zap-threads> element
if (zapRef.current) { if (currentZapRef) {
while (zapRef.current.firstChild) { while (currentZapRef.firstChild) {
zapRef.current.removeChild(zapRef.current.firstChild); currentZapRef.removeChild(currentZapRef.firstChild);
} }
} }
// Append the new element // Append the new element
if (zapRef.current) { if (currentZapRef) {
zapRef.current.appendChild(zapElement); currentZapRef.appendChild(zapElement);
} }
}; };
@ -68,9 +71,9 @@ const ZapThreadsWrapper = ({ anchor, user, relays, disable, className, isAuthori
// Cleanup function to remove the <zap-threads> element and the <script> element when the component is unmounted // Cleanup function to remove the <zap-threads> element and the <script> element when the component is unmounted
return () => { return () => {
if (zapRef.current) { if (currentZapRef) {
while (zapRef.current.firstChild) { while (currentZapRef.firstChild) {
zapRef.current.removeChild(zapRef.current.firstChild); currentZapRef.removeChild(currentZapRef.firstChild);
} }
} }
// Remove the <script> element from the <body> of the document // Remove the <script> element from the <body> of the document