I'm using the OpenWeather REST API which comes in JSON format. I know I have to make my fetch,(Axios in my case) call inside the useEffect because ...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Mitchell, is there any reason why you can't inline
into your
LocalWeather
component and then simply changeaxiosGet
toU talking about just moving the inline function to the LocalWeather component, then just running it like your last block of code?
I just tried that, and I'm getting the following error:
Line 11:8: React Hook useEffect has a missing dependency: 'axiosGet'. Either include it or remove the dependency array react-hooks/exhaustive-deps
I am so sorry, I just realized what you are trying to accomplish. I misunderstood your problem.
As was said before me, you need your KelvinConvert component to return valid JSX in order to be rendered. This can be done by changing its definition to be something like this:
And then passing your Kelvin temperature to it by changing
To
When the useEffect hook finishes, it will update the prop sent to your component.
Please note, I don't actually know what the openWeather object will look like since I haven't used that API, nor do I know specifically what you're trying to get from it, so it is very likely that using
openWeather.kelvin
like I wrote above will not work. Please replace that with the data you want yourKelvinConvert
component to convert.Also, I wrote all of this on my phone, so please forgive any typos or bad code
Sweet, getting 57.938000000000045, so I just need to get that, but that did the trick. Thank you so much.
As far as the object goes with REACT, it's weird, should be
openWeather.main.temp
, but that comes back undefined. Instead, you have to add it asWeird. That's likely because
openWeather
is an empty object until the API call finishes. You might be able to use the new optional chaining operator to get past that! TryAnd see if that works!
As for the float formatting, use the good ol' toFixed method like so
57.15, perfect.
I also had no idea about the optional chaining thing. Good to know thanks. That part looks like it's working as well.
openWeather?.main?.temp
AND
openWeather?.weather?.[0].main --- returns cloud/rain/etc.
You know your stuff. It's much appreciated!
Wow.. I didnt know about the chaining operator.
Thanks
No problem guys! Happy to help.
About optional chaining, it's extremely new, as in I think it just became official standardized this year, so please make sure you're running any code that uses it through babel (if you used create-react-app it should be doing this already) because browser support is going to be terrible
You can do everything in the effect function:
If you get this error:
Line 11:8: React Hook useEffect has a missing dependency: 'KelvinConvert'. Either include it or remove the dependency array react-hooks/exhaustive-deps
You can safely ignore it because since your Effect runs only once, you don't need to keep track of the dependency.
Hi Mitchell... The kevin convert is not returning any jsx... That's why you are getting that error..
If you want to convert and show the farenheit maybe return some
{temperature}
.Also you are not passing any prop to the kelvinconvert component.
So the following doesn't return anything?
return {temperature};
Also, how do you pass info to the props from 1 component to another after the initial fetch/axios call?