Skip to content

Commit

Permalink
Move sharebutton ref into constant
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobArrow committed Dec 10, 2024
1 parent cc76471 commit 4df5e1f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/components/button-share/button-share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ButtonShare: React.FC<ButtonShareProps> = ({ className }) => {
const copyButtonAreaLabel = "Copy this page to clipboard";

const [showFixedButtons, setShowFixedButtons] = useState(true);
const shareButton = useRef<HTMLAnchorElement>(null);
const shareButtonRef = useRef<HTMLAnchorElement>(null);

const onShareButtonClick = () => {
navigator.clipboard.writeText(href);
Expand All @@ -38,13 +38,15 @@ const ButtonShare: React.FC<ButtonShareProps> = ({ className }) => {
{ threshold: 1 }
);

if (shareButton.current) {
observer.observe(shareButton.current);
const shareButton = shareButtonRef.current;

if (shareButton) {
observer.observe(shareButton);
}

return () => {
if (shareButton.current) {
observer.unobserve(shareButton.current);
if (shareButton) {
observer.unobserve(shareButton);
}
};
}, []);
Expand All @@ -71,7 +73,7 @@ const ButtonShare: React.FC<ButtonShareProps> = ({ className }) => {
</div>
)}
<a
ref={shareButton}
ref={shareButtonRef}
href={shareUrl}
aria-label={shareButtonAreaLabel}
className="button-share__button"
Expand Down

0 comments on commit 4df5e1f

Please sign in to comment.