I don’t understand, what should trigger the increase of the number? A click on a button or something? Because if you increase without any condition on every render with useEffect, you’re likely going to end up with an infinite loop and endless re-renders.
yeah the useEffect is not a good idea,
the code above is not finished I just didnt know how to code it further.
its just to show the rest of the code and how i want to use it
It does it the same as JavaScript, there isn’t a special React way to do this. Preferably you use the ID of the product, as that makes the most sense. Or you use the index of the item you’re on in a loop (map gives you that). Or you generate one another way based on something else – that could be anything.
I see, if what he wants is a special key then react does recommend the use of the id, but if you don’t have an id and you really want a special key you can in fact just keep a variable in memory that is a number you increase every time you use it as a key.
This variable would be kept in its own file and you would simply import it for use. There are other methods of accessing the counter, possibly Redux, but just having it in its own file would be sufficient.
You would need a way to update the counter, but I do not imagine it would nee to be a piece if a state.
Yeah if this is just what I think it is – numbering the items in the table – then definitely the index (just index + 1 I guess?). The key just needs to be unique, so the product ID is ideal
Edit: React itself has a small API, and almost everything you use in it is just JS, so I’d suggest making sure you’ve got a really good handle on that would really help w/r/t React.