Data fetching app router
Unanswered
Siamese posted this in #help-forum
SiameseOP
Hi! How should I go about fetching data from an API in a "use client" component? I need to be able to provide data to the API fetch request and the user has to have no access to see the details of the request, so server side fetch.
24 Replies
You would wrap the client component in a server component - make the fetch from the server component, then pass the result down in props to the client component
@josh You would wrap the client component in a server component - make the fetch from the server component, then pass the result down in props to the client component
SiameseOP
Damn that's so overcomplicated. So I need 2 files, right
Are you sure your component needs to be a client component in the first place?
@josh Are you sure your component needs to be a client component in the first place?
SiameseOP
Yup, useEffect
Then yes if you want to avoid making the request from the client you'll need to wrap it in a server component
SiameseOP
Aight
@josh Then yes if you want to avoid making the request from the client you'll need to wrap it in a server component
SiameseOP
So doing something like getServerSideProps on pages router no longer exists?
cause that method was way easier
Server components are the equivalent of getServerSideProps - I find them more intuitive, what are you finding tricky?
SiameseOP
It's not tricky, but getServerSideProps seems like a better solution to me
The app router method is kinda weird
I've heard some mixed reactions!
SiameseOP
Hey @josh really sorry that I ping about this, but, how can i make a update to db in a save popup? the save popup has to be a "use client". I saw some stuff about server actions but not too sure about it beacuse when clicking the save button, i also need to run a client side thing to hide the popup
the app router is so confusing
@Siamese Hey <@227874586312704000> really sorry that I ping about this, but, how can i make a update to db in a save popup? the save popup has to be a "use client". I saw some stuff about server actions but not too sure about it beacuse when clicking the save button, i also need to run a client side thing to hide the popup
Broad-snouted Caiman
You should make the handler an async function. You should create a server action (which also should be an async function) in a separate file and write 'use server' at the beginning of the file. Inside the handler, you call the server action and then set the client state that you want (also inside the handler).
SiameseOP
but im so unhappy about having to like duplicate the page file to make a server side request..
thats kinda dum
@Siamese but im so unhappy about having to like duplicate the page file to make a server side request..
Broad-snouted Caiman
You don't have to create another file just for server actions. I prefer doing that way, but it isn't necessary.
SiameseOP
Talking bout the fetching to display for the user
not saving rn
saving is okay but fetching is weird
Broad-snouted Caiman
You could do something like:
const handler = async () => {
const updateDB = async () => {
'use server'
// code to update the database
}
const { data, error } = await updateDB()
// do something with the return above
setPopUpOpen(false)
}And you pass the handler to the form/button.