{data?.map((item, index) =>
How does the above line of code check for an empty array before mapping? I am not seeing any references for such. Any body please explain?
what does data? do?
Reference here: https://dev.to/madara/fetching-data-with-react-hooks-and-fetch-api-beginners-guide-2ick
.map()method will just return an empty array in return, and the callback function won't be invoked. Thedata?checks that data isn't nullish (undefined / null) before calling .map() on itdata?is optional chaining, ifdatais null/undefined, the.map()method isn't called at all, and yourdata?.map()evaluates toundefined. If data is empty, then.map()still gets called, but it just returns an empty array (and the callback doesn't get executed).