I'm currently working on a quiz project where I fetch an API containing quiz questions and answers. The API provides an array of wrong answers (3) along with one correct answer. My goal is to display these options as buttons, so I decided to push the correct answer into the wrong answers array, randomize them, and present them as choices. However, I encountered an error stating that the array of answers is not iterable. Can anyone assist me in resolving this issue and confirm if my approach is correct?
import './App.css';
import axios from 'axios'
import {useState,useEffect} from 'react'
function App() {
const [quiz,setQuiz] = useState([])
const [answer,setAnswer] = useState([])
useEffect(()=>{
axios.get('https://opentdb.com/api.php?amount=10')
.then(res=>{
setQuiz(res.data.results[0])
setAnswer([...quiz.incorrect_answers, quiz.correct_answer])
})
.catch(err=>{
console.log(err);
})
},[])
return (
<div className="App">
<h1>{quiz.question}</h1>
{answer && answer?.map(answers =>
<button key={answers}>{answers}</button>)
}
</div>
);
}
export default App;