Based on a Graphql variable myKey I query one item of the array of objects:
const { data } = useQuery<GqlRes, Args>(GET_ITEMS, {
variables: {
myKey,
},
errorPolicy: 'all',
});
I have to render a Link in my React jsx:
data.items.map((item) => (
<Link to={item.url}>
{item.title}
</Link>
))}
Is it better to use .map (which in this case returns only one item) or do I have to use something like:
const myLink = data.items[0]
And then use the values like:
<Link to={myLink.url}>
{myLink.title}
</Link>
If you always expect to get only one item, doing data.items[0] is fine but there is not harm in using map either. Using map will keep your code dynamic and will be able to render multiple links should the returned data contain more than one result.