How can I perform a fetch in a server component using its props?
Answered
Brown bear posted this in #help-forum
Brown bearOP
Hi, I have the following server component which is placed in an MDX and that has both params available at build time
What I would like to do it, at build time, check if name is empty, and if it is fetch it from an external url like so
Is this possible?
export default function Spell({ id, name }) {
return (
<>
<a href={`https://www.url.com/item=${id}`} className="inline">
{name || ''}
</a>{' '}
</>
)
}What I would like to do it, at build time, check if name is empty, and if it is fetch it from an external url like so
export default function Spell({ id, name }) {
const displayName = name || await fetch(`www.url.com/getName=${id}`)
return (
<>
<a href={`https://www.url.com/item=${id}`} className="inline">
{displayName}
</a>{' '}
</>
)
}Is this possible?
Answered by Brown bear
Nevermind, I just realized that I can do exactly what I wrote. 🤦♂️
2 Replies
Brown bearOP
Nevermind, I just realized that I can do exactly what I wrote. 🤦♂️
Answer
yeah, you can use prop to fetch data