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

161
Views
Calling setState causes React to throw infinite re-renders error

I was unable to track down the problem, it might be something with the async function or the useEffect hook. If I uncomment the following code I get these errors.

Quizzical.js

import React from 'react'

export default function Quizzical() {
    const [quizzState, setQuizzState] = React.useState([])

    async function getQuestions() {
        let response = await fetch("https://opentdb.com/api.php?amount=5")
        let data = await response.json()
        return data;
    }

    function prepareQuestions(data) {
        let objectArray = []

        data.map(item => {
            let answersArray = []
            item.incorrect_answers.map(ans => answersArray.push(ans))
            answersArray.splice(Math.floor(Math.random() * answersArray.length), 0, item.correct_answer)
          

            objectArray.push({
                answers: answersArray,
                question: item.question,
                correct: item.correct_answer,
                chosen: null
            })
        })

        return objectArray
    }

    React.useEffect(() => {
        getQuestions().then(data => setQuizzState(prepareQuestions(data.results)))
    }, [])

    
    function setAnswer(id, answerID) {
        let obj = quizzState[1]
        console.log(obj)
        // setQuizzState(prev => {
        //     return [...prev]
        // })
    }

    function answerClicked(id, answer) {
        console.log(id, answer)
    }

    setAnswer()

    return (
        <div className="quizzical-app">
            
        </div>
    )
}

Error Message enter image description here

If I keep it commented this is what the console shows: enter image description here

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!