按钮中的条件语句
我是ReactJ的新手,如果达到某个条件,我想禁用此按钮称为“添加项目”。基本上,我要通过按钮中的if和else语句。但是,我对如何做。
const [disable, setDisable] = useState(false);
var quantity = 11;
<Button
if ( quantity > 10 ){ setDisable(true) } // don't know how to properly build here
title="Add Item"
buttonStyle={{backgroundColor: PrimaryColor, borderRadius: 10}}
titleStyle={{fontSize:13}}
disabled={disable}
onPress={InsertItem}
/>
任何助攻都会受到赞赏
I'm new to ReactJs, I have this button called "Add Item" that I would like to disable if a certain condition is reached. Basically I would to pass an if and else statement in the button. However I am stuck as to how to do it.
const [disable, setDisable] = useState(false);
var quantity = 11;
<Button
if ( quantity > 10 ){ setDisable(true) } // don't know how to properly build here
title="Add Item"
buttonStyle={{backgroundColor: PrimaryColor, borderRadius: 10}}
titleStyle={{fontSize:13}}
disabled={disable}
onPress={InsertItem}
/>
Any assist is kindly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要添加任何条件
if(Quantity&gt; 10){setDisable(true)}
然后在onpress
中执行此操作。
中进行操作,或者您可以与依赖关系一起使用变量,以便每次都会调用该变量
的任何变化
disabled
button>按钮
之类的标签中添加条件disabled = {Quantity&gt; 10}
因此,每当您的数量大于10时,它将返回true,并且将与disabled = {true}
相同,而当条件为false时,则相同If you want to add any condition like this
if ( quantity > 10 ){ setDisable(true) }
then do it inonPress
or you can useEffect with dependency variable so that it invokes every time there is any change in that variable
disabled
tag ofButton
like thisdisabled={quantity > 10}
so whenever your quantity is greater than 10 it will return true and it will be the same asdisabled={true}
and same goes for when the condition is false您可能不想禁用将其作为状态存储。如果
数量
是组件状态的道具或内部,则可能足以拥有一个计算的值:&lt; button disabled = {Quantity&gt; 10} .... /&gt; < /code>
You probably don't want to have disabled stored as a state. If
quantity
is a prop or inside the state of your component, it's probably just sufficient to have a calculated value:<Button disabled={quantity > 10} .... />