Next.js Discord

Discord Forum

fetch some data on load of the page

Unanswered
Naeemgg posted this in #help-forum
Open in Discord
 const [tableData,setTableData] = useState(data)
 useEffect(()=>{
 const getData = async()=>{
 const response = await fetch(`/api/get-all-data`)
 const data = await response.json()
 console.log("useEffect called")
 setTableData(data)
 }
 getData()
 },[])
return(<>tableData.map((row,index)=>{
<h1>{row.name}</h1>
})</>)

is this the correct way to do it in appDir??
I'm seeing console logs "useEffect called" everytime I reload the page but not the latest data from db, I'm using postgresql from neon with prisma. (next 13.5)

8 Replies

New Guinea Singing Dog
Might be a caching issue?
Tried on so many dif browsers and machines
New Guinea Singing Dog
Maybe try the fetch like
fetch('/api/getData', {cache: 'no-store`})
 const [tableData,setTableData] = useState([])
    useEffect(()=>{
        const getData = async()=>{
            const response = await fetch(`/api/get-all-data`,{cache:"no-store"})
            const data = await response.json()
            console.log("useEffect called")
            console.log(data)
            setTableData(data)
        }
        getData()
    },[])

still not getting latest records
in localhost everything is fine but not on vercel
Idk what is wrong I'm doing and unable to notice