Next.js Discord

Discord Forum

Fetching data server side | A general doubt

Answered
Naeemgg posted this in #help-forum
Open in Discord
Let's say I'm fetching some data like this:
import React from 'react'
import Component from './Component' 
const getData = async()=>{
  const response = await fetch(`https://jsonplaceholder.typicode.com/todos`,{cache:"no-store"})
  const data = await response.json()
  return data
}
const page = async() => {
     const data = await getData()     
  return (
    <div>    
     <Component tableData={data}/>
    </div>
 )}
export default page

and then in the child component assign the data to a state variable and a dropdown with the number of todos, when a number is selected data has to be changed, I'm not getting how can I do that fetching https://jsonplaceholder.typicode.com/todos/${number} server side. As of now, I'm doing it with useEffect(...) and passing the dependency array as the todo number. But there might be a way to do it while staying server-side correct??
Answered by @ts-ignore
 export default async function Page({searchParams}:{searchParams?:{page?:string}}){
  const req = await fetch(searchParams?.page ? `https://<whatever>/${searchParams.page}`:"https://<whatever>")
}
View full answer

10 Replies

you mean router.push todos number or whatever value we need then make a separate function for that on parent component which is server component , correct??
no
I mean yes but why you need another function
 export default async function Page({searchParams}:{searchParams?:{page?:string}}){
  const req = await fetch(searchParams?.page ? `https://<whatever>/${searchParams.page}`:"https://<whatever>")
}
Answer