Looking to create a Quiz App using React and facing an issue where all buttons change color when clicked, instead of just the one that was clicked. Any solutions on how to make only the clicked button change its color in React.js?
App.js
import Main from './Main'
import Quiz from './Quiz'
import { useState } from 'react'
export default function App() {
const [switch1, setSwitch1] = useState(false)
const [color , setColor] = useState(false)
const qanda = [
{
"question": "In web design, what does CSS stand for?",
"answerOptions" : [ {"answerText" : "Cascading Style Sheet" , "correctAns" : "true" , "id":1 } ,
{"answerText" : "Counter Strike: Source" , "correctAns" : "false" , "id":2 } ,
{"answerText" : "Corrective Style Sheet" , "correctAns" : "flase" , "id":3 } ,
{"answerText" : "Computer Style Sheet" , "correctAns" : "false" , "id":4 } ]
},
{
"question": "Under what pseudonym did Stephen King publish five novels between 1977 and 1984?",
"answerOptions" : [ {"answerText" : "Mark Twain" , "correctAns" : "false" , "id":5} ,
{"answerText" : "Richard Bachman" , "correctAns" : "true" , "id":6} ,
{"answerText" : "J. D. Robb" , "correctAns" : "false" , "id":7} ,
{"answerText" : "Lewis Carroll" , "correctAns" : "false" , "id":8} ]
},
{
"question": "Which modern day country is the region that was known as Phrygia in ancient times?",
"answerOptions" : [ {"answerText" : "Greece" , "correctAns" : "false" , "id":9} ,
{"answerText" : "Syria" , "correctAns" : "false" , "id":10} ,
{"answerText" : "Egypt" , "correctAns" : "false" , "id":11 } ,
{"answerText" : "Turkey" , "correctAns" : "true" , "id":12} ]
},
]
const quizQ = qanda.map(ques => <Quiz question={ques.question} answer1={ques.answerOptions[0].answerText} answer2={ques.answerOptions[1].answerText}
answer3={ques.answerOptions[2].answerText} answer4={ques.answerOptions[3].answerText} click={switchColor} clicked={color} />)
function switchIt() {
setSwitch1(prevValue => !prevValue)
}
function switchColor() {
setColor(prevValue => !prevValue)
}
return (
<div className="body">
{switch1 ? quizQ : <Main onclick={switchIt}/> }
</div>
)
}
...
I am sorry if I have done the techniques wrong this is my first project after learning React