I have modal which, when opened, makes an Axios get request and returns a QR Code, but I'm trying to cache the QR Code, so when the modal is reopened, the QR Code doesn't need to be re-requested.
I've tried something like:
const url = window.location.href;
const qrc = useMemo(async () => {
return await getQRCode(url).then((res) => {
return res.data
});
}, [url]);
Then in my jsx:
{qrc ? <img src={qrc} alt="qr code" /> : <LoadingDisplay />}
But of course, qrc
is still a promise. I'm thinking my syntax is just incorrect, but I couldn't figure it out.
edit:
I've used useEffect with useState, which works, but still re-calls the request every time the modal is re-rendered.
Something like
const [qrc, setQrc] = useState<string>("");
const url = window.location.href;
useEffect(() => {
getQRCode(url).then((res) => {
setQrc(res.data);
});
}, [url]);
The modal is opened and closed with a button click calling setShowModal(!showModal)
with the jsx: {showModal && <Modal />}