Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
Render Modal only when it opens

I am trying to build a modal for 2FA. What I need to do is re-render modal everytime it opens (when you close and open modal again, it will show different QR code).

However, I've encountered one problem. When I try to fetchData() to generate new QR code in useEffect, it throws me an error if I do it inside if (isOpen) statement.

If I do any other action such as console.log inside the same if statement, it works just fine. Problem only occurs when I try to run fetchData() inside the statement.

Error is the following:

Unhandled Runtime Error 
TypeError: Cannot read properties of null (reading 'length')
  69 |   out += String.fromCharCode(0xe0 | charcode >> 12);
  70 |   out += String.fromCharCode(0x80 | charcode >> 6 & 0x3f);
> 71 |   out += String.fromCharCode(0x80 | charcode & 0x3f);
  72 | } else {
  73 |   // This is a surrogate pair, so we'll reconsitute the pieces and work
  74 |   // from that

My code:

const QRModal = ({ isOpen, setIsOpen, setIsTwoStep }) => {

    const { theme, token, handleError, setNotification } = useContext(AuthContext)

    const [authValues, setAuthValues] = useState({
        qr: null,
        secret: null
    })
    const [manual, setIsManual] = useState(false)
    const [isLoading, setIsLoading] = useState(false)

    useEffect(() => {

        const fetchData = async () => {
            await generateQr()
        }
        
        if (isOpen) {
            fetchData()
        }
    }, [isOpen])

Without the if statement, it will re-render even when it closes, leading to generation of new QR code on-close which is not suitable.

Any help and advice would be highly appreciated!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!