0

I am having an array stored in a state. When I try to push new data to array, it is returning this error: Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

This is the code I am using:

const [currencies,setCurrencies] = useState(['USD', 'AUD', 'SGD', 'PHP', 'EURP']); 
setCurrencies(currencies => [...currencies, 'NSD', 'PLN']);
Ajit Kumar
  • 367
  • 1
  • 10
  • 35
ANJANA
  • 27
  • 1
  • 8
  • Can you please provide the full code? Or mention where you are calling the setCurrencies function? – Aniket Kolekar May 30 '21 at 04:07
  • Does this answer your question? [How to push setState array to data?](https://stackoverflow.com/questions/67754822/how-to-push-setstate-array-to-data) – Vasyl Nahuliak May 30 '21 at 18:09

1 Answers1

1

Can you try doing:

setCurrencies([...currencies, 'NSD', 'PLN']);
nishkaush
  • 1,512
  • 1
  • 13
  • 20