Alpha Fiend
Alpha Fiend

Reputation: 65

SWR - Uncaught TypeError: Cannot read properties of undefined

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
  }
}

Upvotes: 3

Views: 2581

Answers (1)

Moein Moeinnia
Moein Moeinnia

Reputation: 2341

Use optional chaining see if that works:

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

Upvotes: 2

Related Questions