Home > Back-end >  SWR - Uncaught TypeError: Cannot read properties of undefined
SWR - Uncaught TypeError: Cannot read properties of undefined

Time:10-13

I'm trying to use SWR for fetching data but I'm getting this error. Not sure why this is happening, though it works fine with use effect hook.

enter image description here

enter image description here

This is the json data where it is fetching from -

{
  "dashboards": {
    "posts": 5,
    "likes": 5,
    "followers": 5,
    "followings": 5
  }
}

CodePudding user response:

Use optional chaining see if that works:

<p>
{data?.posts}
</p>
<p>
{data?.likes}
</p>

  • Related