How can I check in React if a map function is empty or not inside the return section? wanna check If it’s empty, if so render an empty array, otherwise wanna render everything by mapping the content. I am asking because rendering an empty array this way will cause an exception..
return (
{functionName.map((…}
)
{functionName.map.lengh().? …}
)
2
Answers
Looks like the quickest way around this is by adding a "?" mark to the function name like this?
{functionName?.map.((item)=>...}
Is your function returning an array?
Why not store what it returns in a usestate and map directly from the state using this
state?.map(your map goes here)
.That way, it will be more easier for you, cleaner and more readable.