'React cannot read properties of undefined inside conditional statement
I'm fetching data from an API which returns an object that looks like this.
results: {
AT: {
buy[],
rent[]
},
US: {
buy[],
rent[]}
}
And I'm having trouble checking whether the property US exists. This is resulting in a cannot read properties of undefined error. The way I'm attempting to do it looks like this.
{typeof(provider?.results.US.flatrate) !== undefined && provider?.results.US.flatrate.map()}
The data is stored inside provider using the useState hook.
const [provider, setProvider] = useState<Monetization>();
The way I'm fetching data from the API looks like this.
axios.get("https://api.themoviedb.org/3/movie/" + movieId +
"/watch/providers?api_key=my_key").then(res => setProvider(res.data));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
