data becomes undefined (empty array) after re-renders in Next.js #1601
-
Good day, everyone! :) I'm not sure if problem's related to SWR, but it happened after I migrated from ^0.5.6 to ^1.0.1 ver. the day before. I have an article page where I fetch article on server-side in getStaticProps, pass it as props to page and set it as fallbackData. pages/articles/[slug].js:
The page loads successfully but after a moment it becomes blank and says that data's undefined: Console shows that the data object becomes empty after re-renders (unlike article and fallbackData objects): When I downgrade SWR to ^0.5.6 everything is okay. Does anybody know why is it like that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi! url => fetch(url).then(res => res.json()).then(data => {
console.log(data)
return data
}) And you will probably see that the fetched data is an empty array. It's not undefined. This is the correct behavior, for the initial render there is no data, so SWR returns the fallback. And then SWR fetches the data (gets an empty array), and returns that data instead of the fallback. |
Beta Was this translation helpful? Give feedback.
Hi!
The screenshots are broken, butcan you put a logger here:And you will probably see that the fetched data is an empty array. It's not undefined.
This is the correct behavior, for the initial render there is no data, so SWR returns the fallback. And then SWR fetches the data (gets an empty array), and returns that data instead of the fallback.