Is there a way to make the correct
button hard coded in the Question component render between the mapped incorrect buttons to prevent the correct answer from always being the first button rendered with every question? Or a way to map through both correct and incorrect answers to display the answers with random positions, the question array comes from the API in this form:
{
question: "The words and originate from the languages of which country?",
correctAnswer: "India",
incorrectAnswers: ["Papua New Guinea", "Ethiopia", "China"]
}
const [request, setRequest] = React.useState([])
React.useEffect (() => {
fetch('https://opentdb.com/api.php?amount=5')
.then(res => res.json())
.then(data => setRequest(data.results.map(({ question, correct_answer, incorrect_answers }) => ({question, correct_answer, incorrect_answers}))))
}, [])
console.log(request)
const questionElements = request.map(req => {
return (
<Question
question = {req.question}
correct_answer ={req.correct_answer}
incorrect_answers = {req.incorrect_answers}
scoreFunction = {scoreFunction}
// disabled = {disable}
/>
)
})
// Question Component
const incorrectAnswers = props.incorrect_answers.map(ans => {
return (
<button className ="button">{ans}</button>
)
})
return(
<div className = "question-div">
<h1 className = "question">{props.question}</h1>
<div className = "answerBlock">
<button
disabled = {disable}
className ="button correct"
onClick = {() => {
props.scoreFunction()
setDisable(true)}}>{props.correct_answer} </button>
{incorrectAnswers}
</div>
<hr />
</div>
)