Fetching data server side | A general doubt
Answered
Naeemgg posted this in #help-forum
NaeemggOP
Let's say I'm fetching some data like this:
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
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 pageand 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>")
}10 Replies
and in your server component, check if query param is present or not
then proceed as you need
NaeemggOP
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??
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