I’m using react and trying to display different behaviour in order if user’s channel is online or not.
So i get the response but the following code below doesn’t work as it’s always return first statement rather than else even if channel is offline ( in this case that is when array length 0 which is case when user is not online - there is no object )
This is my code
Check out line 35 and 38 - at 35 in the console.log it shows that in the state that length is equal to zero but somehow it always displays this insted of one below in else statement.
{ data: [], pagination: {} }
undefined
Is not zero
The use of !== performs a test and a type cast test. So you’ve tried to see if an OBJECT has length. And of course an object doesn’t
Line 12:
this.setState({data: response.data})
Should be:
this.setState({data: response.data.data})
Response.data fetches the response from twitch, but you probably want the data array in the respone that’ll contain 0/1 streams in an array which is what you can test the length of as it’s an Array.
I knew it wasn’t but i was not properly sure if i’m calling it and by that i mean with response.data as when i looked into console to debug it looked right.