按钮中的条件语句

发布于 2025-02-07 10:35:36 字数 543 浏览 1 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无力看清 2025-02-14 10:35:36
  1. 如果要添加任何条件if(Quantity&gt; 10){setDisable(true)}然后在onpress

    中执行此操作。

  2. 中进行操作,或者您可以与依赖关系一起使用变量,以便每次都会调用该变量

    的任何变化

     React.useEffect(() => {
        if (quantity > 10) {
          setDisable(true)
        }
      }, [quantity])
  1. ,或者只是在disabled button>按钮之类的标签中添加条件disabled = {Quantity&gt; 10}因此,每当您的数量大于10时,它将返回true,并且将与disabled = {true}相同,而当条件为false时,则相同
    const [disable, setDisable] = useState(false);
    var quantity = 11;
    
       <Button
        title="Add Item"
        buttonStyle={{backgroundColor: PrimaryColor, borderRadius: 10}}
        titleStyle={{fontSize:13}} 
        disabled={quantity > 10}
        onPress={InsertItem}                     
       />
  1. If you want to add any condition like this if ( quantity > 10 ){ setDisable(true) } then do it in onPress

  2. or you can useEffect with dependency variable so that it invokes every time there is any change in that variable

     React.useEffect(() => {
        if (quantity > 10) {
          setDisable(true)
        }
      }, [quantity])
  1. or simply just add a condition for disable in the disabled tag of Button like this disabled={quantity > 10} so whenever your quantity is greater than 10 it will return true and it will be the same as disabled={true} and same goes for when the condition is false
    const [disable, setDisable] = useState(false);
    var quantity = 11;
    
       <Button
        title="Add Item"
        buttonStyle={{backgroundColor: PrimaryColor, borderRadius: 10}}
        titleStyle={{fontSize:13}} 
        disabled={quantity > 10}
        onPress={InsertItem}                     
       />
梦太阳 2025-02-14 10:35:36

您可能不想禁用将其作为状态存储。如果数量是组件状态的道具或内部,则可能足以拥有一个计算的值:

&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} .... />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文