[英]How can I prevent the button from triggering the other button when I click it? &React
My problem is when I click my watched button, it also triggers handler button.我的问题是当我单击我的监视按钮时,它也会触发处理程序按钮。 How can I prevent that?
我怎样才能防止这种情况?
Here is my buttons code: HTML:这是我的按钮代码:HTML:
<div className="bot">
<button id="handler" onClick={() =>{handleClick(0)}}>PICK IT</button>
<button id="watched" onClick={() =>{watched()}}>I Already Watched This</button>
<a href={data.trailer} target = "_blank" className = "button">WATCH TRAILER</a>
</div>
React & Firebase part:反应和 Firebase 部分:
function watched(){
const db = getDatabase(app);
update(ref(db,"users/" + userInfo + "/watched/" + data.id),{"watched" : "true"});
}
function handleClick(val){
if (val >= 250){return 1;}
const numberOfUsers = 250;
const randomIndex = Math.floor(Math.random() * numberOfUsers);
const database = getDatabase(app);
const watched = ref(database,"users/" + userInfo + "/watched/" + randomIndex);
onValue(watched,(snapshot) =>{
if (snapshot.val().watched === "true"){
handleClick(val+1)
}
else{
const starCountRef = ref(database, 'movies/');
onValue(starCountRef, (snapshot) => {
const data = snapshot.val();
setData(data[randomIndex])
});}
})
I solved the problem like this:我解决了这样的问题:
function handleClick(val){
console.log("handlerrr");
if (val >= 250){return 1;}
const numberOfUsers = 250;
const randomIndex = Math.floor(Math.random() * numberOfUsers);
const database = getDatabase(app);
const watched = ref(database,"users/" + userInfo + "/watched/" + randomIndex);
onValue(watched,(snapshot) =>{
const val = snapshot.val() <-------- THIS LINE ADDED
if (val === "true"){
handleClick(val+1)
}
else{
const starCountRef = ref(database, 'movies/');
onValue(starCountRef, (snapshot) => {
const data = snapshot.val();
setData(data[randomIndex])
});}
})
I am not sure but probably problem was when onValue worked after that my handleClick function was listening my database and after each update on my database it was triggering.我不确定,但可能问题是当 onValue 工作之后,我的 handleClick function 正在监听我的数据库,并且在我的数据库每次更新后它都会触发。 So I added const val = snapshot.val() now it is only rendering 1 time.
所以我添加了 const val = snapshot.val() 现在它只渲染了 1 次。
Just add event inside onClick function onClick={(e) =>{handleClick(e, 0)}
.只需在 onClick function
onClick={(e) =>{handleClick(e, 0)}
中添加事件。 Adding this event you will prevent triggering button without actual click.添加此事件将防止在没有实际点击的情况下触发按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.