简体   繁体   English

React Js:我如何使用索引号来禁用按钮(如果索引 = 4 则禁用按钮)

[英]React Js : How can i use index number to disable button ( if index = 4 then disable button )

enter image description here i have to create a component where one person can add upto 4 addresses for doing this i have used slice(0,4) in map function but i have to make button disabled when 4 addresses are entered在此处输入图像描述我必须创建一个组件,其中一个人最多可以添加 4 个地址来执行此操作 我在 map function 中使用了 slice(0,4) 但我必须在输入 4 个地址时禁用按钮

How can i do this i need a solution我该怎么做我需要一个解决方案

enter image description here在此处输入图像描述

I think you looking for something like this in your return statement:我想你在你的退货声明中寻找这样的东西:

        <Button
          className="btn-info btn-sm"
          type="button"
          onClick={(e) => clicked(e)}
          disabled={locations.length >= 4}
        >
          Add Address
        </Button>

I think you want to use map.size() for the number of addresses in your disable conditional instead of index.我认为您想使用 map.size() 作为禁用条件而不是索引中的地址数。

Note that I have been usingBootstrap components so the button is a drop-in widget.请注意,我一直在使用Bootstrap组件,因此该按钮是一个嵌入式小部件。 If your not using a UI widget library then you should consider it to make things easier.如果您不使用 UI 小部件库,那么您应该考虑使用它来简化操作。

if(locations.length < 4) { <button type="button"> Add </button> } So basically the button will appear or dissapear based on if the condition is true or false. if(locations.length < 4) { <button type="button"> Add </button> }所以基本上按钮会根据条件是真还是假来显示或消失。

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

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