kminny
kminny

Reputation: 239

Cannot read property of undefined on react

I am setting up a client code and the error "Cannot read property '' of undefined" keeps occur.

TypeError: Cannot read property ' ' of undefined

It is interesting that if I refresh the page several times then suddenly it works! For example, the home page is random within 10 times, menu page shows me an error page at the first time, half loaded at first refresh, fully loaded at second refresh.

I think props can't get data correctly right away. So I think I can solve this problem by refreshing automatically until props get correct data.

Is there any way refreshing until I get the props?

Or any other solution about this problem?

menu error

enter image description here

home error

enter image description here

Upvotes: 0

Views: 2452

Answers (3)

bobbylemm
bobbylemm

Reputation: 42

Yea, you will need to confirm that the data object exists before doing a conditional. so

loading || {data || !data.me || !data.getPlaces}

Upvotes: 0

Vadim Hulevich
Vadim Hulevich

Reputation: 1833

First you need check exist of data, and then fields in this object:

loading || (data && (!data.me || !data.getPlaces)) ? ...

Upvotes: 1

Igor S
Igor S

Reputation: 969

Try to change your if condition to:

{loading || !data || !data.me ...

Upvotes: 0

Related Questions