如何使用PressableonPress属性或任何其他属性或任何替代方法传递道具



我正试图将(映射答案的(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);
}
}

相关内容

  • 没有找到相关文章

最新更新