我正试图将(映射答案的(answer
字符串传递到handleButtonClick
函数中,在那里我将把答案与正确答案进行比较。以下是创建答案字符串的代码:
<View>
{answers.map(answer => (
<View key={answer}>
<Pressable
onPress={handleButtonClick} >
<Text>
{answer}
</Text>
</Pressable>
</View>
))}
</View>
这是handleButtonClick:的代码
const handleButtonClick= () => {
//check answer due to correct answer
const correct = questions[number].correct_answer === answer; // I am trying to pass the "answer" variable to here in order to make the comparison
console.log("isCorrect = ", correct)
//add score if answer is correct
if (correct) {
setScore(prev => prev + 1);
}
};
像这样传递:
<Pressable onPress={() => handleButtonClick(answer)} >
const handleButtonClick = (answer) => {
const correct = questions[number].correct_answer === answer;
console.log("isCorrect = ", correct)
//add score if answer is correct
if (correct) {
setScore(prev => prev + 1);
}
}