[英]Disable link based on input values: ReactJS
I noticed that disable is not working as expected for component in REACT.我注意到禁用对于 REACT 中的组件没有按预期工作。
<Link onClick={() => this.DoSomething()}
disabled={this.props.status != "Approved"}>
<NavDropdown.Item>Approve</NavDropdown.Item>
</Link>
I want to stop the traversal of link for particular condition.我想停止特定条件下的链接遍历。 --For status =Approved only in this case i want it to call this.DoSomething() --For status =Approved 仅在这种情况下我希望它调用 this.DoSomething()
With ternary(conditional) operator condition, i am unable to compare in the LINK component.使用三元(条件)运算符条件,我无法在 LINK 组件中进行比较。
I cant not put entire link component in conditional operator as i need NavBar for both the scenarios.我不能将整个链接组件放在条件运算符中,因为这两种情况都需要 NavBar。
OnClick
will be called whether disabled
is true or not.无论disabled
是否为真,都会调用OnClick
。 If you want to prevent traversal you can do something like this inside DoSomething
如果你想防止遍历,你可以在DoSomething
中做这样的事情
DoSomething = (e) => {
if(this.props.status != "Approved") {
e.preventDefault();
}
}
You should define a variable to use in both in the disabled and the onClick function:您应该定义一个在禁用和 onClick function 中使用的变量:
const disabled = this.props.status != "Approved"
<Link onClick={() => disabled ? return : this.DoSomething()}
disabled={disabled}>
<NavDropdown.Item>Approve</NavDropdown.Item>
</Link>
Disabled is only a boolean, it will never effect your added eventListener onClick. Disabled 只是一个 boolean,它永远不会影响您添加的 eventListener onClick。 https://www.w3schools.com/tags/att_disabled.asp#:~:text=Definition%20and%20Usage,a%20checkbox%2C%20etc. https://www.w3schools.com/tags/att_disabled.asp#:~:text=Definition%20and%20Usage,a%20checkbox%2C%20etc。 ). )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.