[英]How do I disable only the Clicked button for a mapped over component
I am mapping over the request
variable and rendering it on the page, it has an array of object with a question
, correct_answer
and incorrect_answer
properties, and I also set a state named disable
that is toggled between true and false when the correct_answer
button is clicked to make the correct answer button get disabled or unclickable for each question, but clicking the correct answer button for ONE question disables all correct answer buttons for the OTHER questions我正在映射
request
变量并将其呈现在页面上,它有一个带有question
、 correct_answer
和incorrect_answer
属性的对象数组,并且我还设置了一个名为disable
的状态,当单击correct_answer
按钮时,该状态在 true 和 false 之间切换使每个问题的正确答案按钮被禁用或不可点击,但单击一个问题的正确答案按钮会禁用其他问题的所有正确答案按钮
I basically want to disable the correct answer button on click or making it clickable once我基本上想在单击时禁用正确答案按钮或使其可单击一次
const [request, setRequest] = React.useState([]) const [disable, setDisable] = React.useState(false); function scoreFunction (event){ setDisable(true) setScore(prevScore => prevScore + 1) } 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} /> ) }) // The Question Component export default function Question (props){ 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 = {props.disabled} className ="button correct" onClick = {props.scoreFunction}>{props.correct_answer} </button> {incorrectAnswers} </div> <hr /> </div> ) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
Essentially the issue is because your disabled useState
is in the wrong place.本质上问题是因为您禁用的
useState
位于错误的位置。 It needs to be inside the Question
component so you have 1 disabled flag per question rather than 1 global one.它需要位于
Question
组件内,因此每个问题都有 1 个禁用标志,而不是 1 个全局标志。
export default function Question (props) {
const [disabled, setDisabled] = useState(false)
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 = {disabled}
className ="button correct"
onClick = {() => {
setDisabled(true)
props.scoreFunction()
}>{props.correct_answer} </button>
{incorrectAnswers}
</div>
<hr />
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.