简体   繁体   English

当禁用为真时,为什么不反应禁用 onClick 处理程序?

[英]Why doesn't react disable the onClick handler when disabled is true?

I would expect that setting the disabled attribute on a react component would block the onClick handler for that element.我希望在反应组件上设置禁用属性会阻止该元素的 onClick 处理程序。

 <a role="button"
        className={`btn btn-block btn-info`}
        disabled={!this.state.readyToView}
        href={this.state.selectedObjectLink}
        onClick={this.handleLink}
        >View</a>

but although the element shows a "disabled" attribute it still registers a click event.但是尽管该元素显示了“已禁用”属性,它仍然会注册一个点击事件。

Edit: I should clarify - I handle the click event in handleLink, I want to know why the disabled attribute doesn't remove the handler?编辑:我应该澄清一下——我在 handleLink 中处理点击事件,我想知道为什么 disabled 属性没有删除处理程序? Sorry for any confusion.抱歉造成任何混乱。

The problem isn't with disabled ;问题不在于disabled it's with the HTML element a .它与 HTML 元素a Anchor <a> cannot have a disabled property (what does that even mean?).<a>不能具有禁用属性(这甚至意味着什么?)。 Just because you've passed CSS to make the element look like a button, doesn't make it a button.仅仅因为您已经通过 CSS 使元素看起来像一个按钮,并不能使它成为一个按钮。 It is still an anchor.它仍然是一个锚。

The solution would be either to use something else (like button):解决方案是使用其他东西(如按钮):

<button 
  role="button"
  className={`btn btn-block btn-info`}
  disabled={!this.state.readyToView}
  onClick={this.handleLink}
>
  View
</button>

You can use this.state.selectedObjectLink inside handleLink if you want to redirect to a page如果你想重定向到一个页面,你可以在handleLink使用this.state.selectedObjectLink

Or use one of the many other suggestions on this page.或者使用此页面上的许多其他建议之一。

Why not just handle this in handleLink ?为什么不直接在handleLink处理呢?

handleLink () {
  if (!this.state.readyToView) return
  // ...
}

If you really want to bind/remove the handler dynamically, you can do something like this:如果你真的想动态绑定/删除处理程序,你可以这样做:

const clickHandler = this.state.readyToView ? this.handleLink : null
...
<a role="button"
  ...
  ...
  onClick={clickHandler}
>View</a>

You can add a condition in your click handler, something like this您可以在点击处理程序中添加一个条件,就像这样

<a role="button"
        className={`btn btn-block btn-info`}
        disabled={!this.state.readyToView}
        onClick={this.state.readyToView && this.handleLink}
        >
        View
</a>

jsfiddle提琴手

If you are using react version 16 and up如果您使用的是 React 16 及更高版本

on onClick don't call the method directly use () => method instead like this在 onClick 上不要直接调用方法,而是像这样使用 () => 方法

const handleRemoveBtnClick = () => {
 ...
}

<button ...onClick={() => handleRemoveBtnClick} />

Another trick to disable the click to any components is to use useState with boolean value either true to disable or false to resume the click functionality禁用对任何组件的点击的另一个技巧是使用带有 boolean 值的 useState true 禁用或 false 恢复点击功能

example例子

export default const ElementComponent() =>{

    //set the initial value of disable click to false
    const [disableClick,setDisableClick] = useState(false)
 
    const anotherFunction =() =>{
        console.log("I've been run")
    }

    const handleClick()=>{
        //if disableClick is false then run this if block, else don't do anything
        if(!disableClick){
            anotherFunction()
        }
    }

    return(
        <img src="..." onClick={handleClick}/>
    )
}

the good thing about this is that we can pass the function as a props to another components and we can run it there, we can even let's say disable a click for let's say 1 second using setTimeout function, there are tons of way you can use this method, hope it helps anyone if you want to use onClick not only on the button element itself这样做的好处是我们可以将 function 作为道具传递给另一个组件,我们可以在那里运行它,我们甚至可以说使用 setTimeout function 禁用点击,假设 1 秒,有很多方法可以使用这种方法,如果你想使用 onClick 而不仅仅是按钮元素本身,希望它对任何人都有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM