#Need some help trying to figure out how to pass URL prop for component displaying fetched data
23 messages · Page 1 of 1 (latest)
Need more information to understand what you are trying to do
Like how is the user gonna enter the URL and anything else that could possibly help
Use an input form to enter the url
So user starts off with a form to enter URL, url gets passed in to fetch data. Then once data is fetched, I want to use the LinkMetadata component to display it by the entered URL if that makes sense.
The button and input form are set up, but I can’t figure out how to make the component pass a URL prop that fetches data from the url
So are you getting any errors or does it just not behave like you expect it to?
Not behaving like I expect it to, I’m think there’s something wrong with the above code
Does the fetch request work like you expect it to?
Which part is it that isn’t behaving correctly
It’s a bit difficult if there’s no error and I can’t run the code
The error occurs in the useEffect(), I don’t think the way I wrote it is correct
The fetch request is fine if I ignore the other parts and try to get the data out it works. I think my logic doesn’t make sense for what I’m trying to do
This is the API I’m calling it from, https://jsonlink.io/api/extract?url= and I want the URL to be passed in the LinkMetadata prop so I can populate the metadata information I get from the fetch
oh your useffect needs an empty array as a second argument
try {
setError(null)
const metadata = getMetaData(url)
setMetadata((metadata) =>
{
title: metadata?.title,
description: metadata?.description,
images: metadata?.images[],
domain: metadata!.domain,
url: metadata.url,
}
)
} catch (error: any) {
console.log('error', error.message)
setError(error.message)
}
},[])```
hmm, didn't seem to work, says my title, description, ... are undefined
cand you post a screenshot of the error?
where are you getting the getMetadata function from?
ahh I changed the getStaticProps because someone said it wasn't useable if it's not within Pages, but found that I can actually use it
yeh you can only use it if its a page
so I can replace it with a function like getMetadata?
I think I figured it out with using the useEffect hook