简体   繁体   English

当我单击它时,如何防止按钮触发另一个按钮? 反应

[英]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.

相关问题 防止空格按钮触发jQuery中的任何其他按钮单击 - Prevent space button from triggering any other button click in jQuery 如何阻止子按钮触发 React 中的父链接? - How can I stop a child button triggering a parent Link in React? react native:当我从另一个页面单击按钮时,如何更改视图? - react native: How can I change a view when i click a button from another page? 连续两次单击触发链接时,如何防止click事件执行相同的查询 - How can I prevent a click event from executing the same query when the triggering link is clicked twice in a row 防止点击 label 触发子按钮 - Prevent click on label from triggering child button 单击打印按钮时如何防止打开新标签页 - How to prevent opening a new tab when I click the print button 单击按钮时如何防止我的页面呈现 - How to prevent my Page render when I click on the button 当实际鼠标单击没有在目标元素上开始时,如何防止触发单击事件? - How can I prevent triggering an on click event when the actual mouse click didn't start on the target element? 我如何才能防止onclick按钮多次单击,直到通过javascript if(confirm)条件进行确认 - How can i prevent onclick button from multiple click until it's confirmation from javascript if(confirm ) condition 我如何做“当我单击其他按钮计数器更改时” - How can I do “ when I click other button counter change”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM