-2

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

How can i do this i need a solution

enter image description here

  • Welcome to SO. You might find reading the site [help section](https://stackoverflow.com/help) useful when it comes to [asking a good question](https://stackoverflow.com/help/how-to-ask), and this [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Code that you have worked on to solve the problem should include a [mcve], and be included in your question. [Here's some documentation on how to create a React snippet](https://meta.stackoverflow.com/a/338538/1377002). – Andy Apr 10 '22 at 18:12
  • Don't use indices. Your buttons are "things", they exist for a reason, so update your state based on user input, and then just make sure to render a new virtual dom with whatever the tree needs to look like (with `key` attributes, of course, so that React can do all the "which bits to keep, which bits to throw away, which bits to modify-in-place"). You are responsible for saying what things should look like, and React is responsible for updating the UI accordingly. Don't try to do React's job in your own code =) – Mike 'Pomax' Kamermans Apr 10 '22 at 19:05

2 Answers2

0

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.

Note that I have been using Bootstrap components so the button is a drop-in widget. If your not using a UI widget library then you should consider it to make things easier.

Cheshiremoe
  • 792
  • 1
  • 5
  • 14
  • i have attached what i have to do is all i have to do within map function {locations.slice(0, 4).map((location, index) => ( use that index value to toggle buttons state such that if index=== 4 then button disabled if less than 4 button enabled – Shreyash Giri Apr 10 '22 at 18:18
  • or how can i export index number from map function – Shreyash Giri Apr 10 '22 at 18:20
  • i am quite confused is there any way we could connect verbally i really need help – Shreyash Giri Apr 10 '22 at 18:26
  • I have been editing my answer several times. Please look at it again and explain what part your confused about. I don't think you want to use the index of the last location, locations size is a much easier to access out side of the map function. Is the location variable an array? – Cheshiremoe Apr 10 '22 at 18:41
  • yeah its an array – Shreyash Giri Apr 10 '22 at 18:43
  • then you can simply use locations.length to get the number of items in the array. – Cheshiremoe Apr 10 '22 at 19:01
0

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.