I am using a functional component to print out some data in React using a basic if statement
import React from 'react'
export default function Weather (props) {
let obj = props.data
if(obj === undefined || {}) {
console.log(obj) <---returns desired object
return <div>Enter in a city, zipcode, or address</div>
} else {
console.log(obj) <-- returns undefined
return<div>
<p>{obj.minutely.summary}</p>
<p>{obj.currently.temperature}</p>
<p>{obj.currently.humidity}</p>
<p>{obj.currently.windSpeed}</p>
<p>{obj.currently.uvIndex}</p>
<p>{obj.currently.precipProbability}</p>
</div>
}
}
The logic of the if statement is as such: if the data is undefined
print out directions to enter an address, otherwise print out the data.
Confusingly enough, what is happening is that the if statement appears to be ignoring the conditional, because
if(obj === undefined || {}) {
console.log(obj)
return <div>Enter in a city, zipcode, or address</div>
}
this console.log
returns all my data even though its only supposed to run if the object is undefined.
The second if statement doesn't even print out. I've tried switching the two, so that it looks like so:
if(obj !== undefined || {}) {
return<div>
<p>{obj.minutely.summary}</p>
<p>{obj.currently.temperature}</p>
<p>{obj.currently.humidity}</p>
<p>{obj.currently.windSpeed}</p>
<p>{obj.currently.uvIndex}</p>
<p>{obj.currently.precipProbability}</p>
</div>
} else {
return <div>Enter in a city, zipcode, or address</div>
}
However, because it is trying to look for an object that has not yet been set it still stops on the first if statement and rings as undefined(although that is expected)
Any suggestions?