Skip to content

Commit

Permalink
QR code width now uses react ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen-Gordon committed Apr 7, 2024
1 parent f8537e1 commit fc03a18
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/app/components/Qr/Qr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { QRCode } from 'react-qrcode-logo';
// Redux
import { useSelector } from 'react-redux';
// react
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef } from 'react';
import { RootState } from '@/GlobalRedux/store';

export default function Qr() {
Expand All @@ -14,29 +14,22 @@ export default function Qr() {

const [windowWidth, setWindowWidth] = useState(0);

const widthRef = useRef<HTMLDivElement>(null);

useEffect(() => {
function handleResize() {
const screenWidth = window.innerWidth; // Get the screen width
const tenPercentOfScreenWidth = screenWidth * 0.3; // Calculate 10% of the screen width
const screenWidthMinusTenPercent = screenWidth - tenPercentOfScreenWidth; // Subtract 10% from the screen width
setWindowWidth(screenWidthMinusTenPercent); // Set the state with the modified screen width
if (widthRef.current) {
const width = widthRef.current.offsetWidth;
console.log('width', width);
setWindowWidth(width);
}

// Initial width on component mount
handleResize();

// Event listener for window resize
window.addEventListener('resize', handleResize);

// remove the event listener when the component unmounts
return () => window.removeEventListener('resize', handleResize);
}, []);

return (
<>
<div ref={widthRef}>
<QRCode
ecLevel='H'
quietZone={20}
size={windowWidth > 500 ? 300 : windowWidth }
size={windowWidth > 500 ? 300 : windowWidth}
bgColor='#0c0e1800'
fgColor='#a7a3f5'
qrStyle='dots'
Expand All @@ -45,6 +38,6 @@ export default function Qr() {
eyeColor={['#ec91d8', '#ec91d8b3', '#ec91d866 ']}
value={`${address}`}
/>
</>
</div>
);
}

0 comments on commit fc03a18

Please sign in to comment.