[英]REACT : How to deal with disable onclick event
While mapping array, i want to check other elements' classlist in the array.在映射数组时,我想检查数组中其他元素的类列表。 Want to stop manipulate classlist if any button clicked before.
如果之前单击过任何按钮,想要停止操作类列表。 Here is the code and issue photo
这是代码和问题照片
const questionElements = questions.map((question) => {
// ANSWERS ARRAY AND SHUFFLE
let answers = [...question.incorrect_answers,question.correct_answer]
shuffle(answers)
// TOGGLE BUTTON BACKGROUND COLOR WHEN BTN CLICKED
function toggleBtnBgColor(btn) {
btn.target.classList.toggle("dark-bg-color")
}
return (
<div key={nanoid()}>
<div className="question" key={nanoid()}>{question.question}</div>
{answers.map((answer)=> {
return (
<button key={nanoid()} onClick={toggleBtnBgColor} className="answer-button">{answer}</button>
)
})}
<hr></hr>
</div>
)
})
return (
<div className="question-container">
{questionElements}
</div>
)
Check please this codesandbox .请检查 此代码框。 It can be a little complicated for you but feel free to ask questions
对您来说可能有点复杂,但请随时提出问题
We use useState
as a store for keys and answers flags.我们使用
useState
作为键和答案标志的存储。 When we update our store via setQuestions
with each update we calculate answersDisabled
and pass it through props in answer component.当我们通过
setQuestions
更新我们的商店时,我们计算answersDisabled
并通过 answer 组件中的 props 传递它。
Example isn't perfect but I hope it helps you示例并不完美,但希望对您有所帮助
I am still confused about some part of my react code.我仍然对我的反应代码的某些部分感到困惑。 I decided to seperate the data from api instead store questions in state.
我决定将数据从 api 中分离出来,而不是将问题存储在 state 中。 Then i plan to build question object and set questions and boolean flag for answer buttons.
然后我计划构建问题 object 并为答案按钮设置问题和 boolean 标志。 Recent issue is that when i get data from api, repeats itself 4 times.
最近的问题是,当我从 api 获取数据时,会重复 4 次。 Let me show you code and console.
让我向您展示代码和控制台。 Many thanks for help.
非常感谢您的帮助。
const [data, setData] = React.useState()
// FETCH QUIZ DATA FROM API
const fetchData = () => {
fetch("https://opentdb.com/api.php?amount=5&type=multiple")
.then((response) => response.json())
.then((data) => setData(data.results))
}
console.log(data)
React.useEffect(()=> {
fetchData()
},[])
SOLVED.解决了。 Seperated Question component from Quiz.js.
来自 Quiz.js 的分离问题组件。 Passed needed props to the components.
将所需的道具传递给组件。 Many thanks for all.
非常感谢大家。 Here is code
这是代码
import React from "react"
import { nanoid } from "nanoid"从“nanoid”导入 { nanoid }
export default function Question(props) {导出默认 function 问题(道具){
const [flag,setFlag] = React.useState(true)
// TOGGLE BUTTON BACKGROUND COLOR WHEN BTN CLICKED
function toggleBtnBgColor(btn) {
if(flag) {
btn.target.classList.toggle("dark-bg-color")
setFlag(false)
}
}
return (
<>
<div className="question" key={props.index}>{props.question}</div>
{props.answers.map((answer,index)=> {
return (
<button key={index} onClick={toggleBtnBgColor} className="answer-button">{answer}</button>
)
})}
<hr></hr>
</>
)
} }
Here is Quiz.js这是 Quiz.js
import React from "react"
import Question from "./Question"从“./Question”导入问题
export default function Quiz() {导出默认 function 测验(){
const [data, setData] = React.useState([])
// FETCH QUIZ DATA FROM API
const fetchData = () => {
fetch("https://opentdb.com/api.php?amount=5&type=multiple")
.then((response) => response.json())
.then((data) => setData(data.results))
}
React.useEffect(()=> {
fetchData()
},[])
// SHUFFLE ALGORITHM TO USE LATER
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
const questionElements = data.map((question, index) => {
// ANSWERS ARRAY AND SHUFFLE
let answers = [...question.incorrect_answers,question.correct_answer]
shuffle(answers)
return (
<Question key={index} index={index} question={question.question} answers={answers}/>
)
})
return (
<div className="question-container">
{questionElements}
</div>
)
} }
I don't quite understand your issue.我不太明白你的问题。 What you mean by stop manipulating classlist?
停止操纵类列表是什么意思?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.