I have a functional react component that displays information after an api request. Initially the object it displays is blank, since the request hasn't been made. In the object is an array that looks like this:
['US', 'USA', 'United States of America']
When I simply display the array, after the api request, it displays it as a single string on the page with no spaces between ex:
USUSAUnited States of America
Naturally, I want to format it with .join(', ' ) to format the string like US, USA, United States of America, but after I add the .join(', '), it throws an error:
TypeError: props.modalCountry.alt_spellings is undefined
It seems like .join() is trying to run on first render before there is an actual modalCountry object. How do I get this method to not run until the object & array actually exists?
join) on non-array objects, which would fail equally as badly or worse.