DEV Community

Discussion on: Run Function after useEffect to Manipulate Data?

Collapse
taylorbeeston profile image
Taylor Beeston

Hi Mitchell, is there any reason why you can't inline

const convertKelvinToFahrenheit = kelvin =>
  (kelvin - 273.15) * 9/5 + 32;

into your LocalWeather component and then simply change axiosGet to

const axiosGet = () => {
        axios.get(`https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=${process.env.REACT_APP_WEATHER_KEY}`)
        .then(data => setWeather(convertKelvinToFahrenheit(data.data)));
    }
Collapse
mitchelln11 profile image
mitchelln11 Author

U 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

Collapse
taylorbeeston profile image
Taylor Beeston

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:

import React from 'react';

const KelvinConvert = ({ kelvin }) => {
  const kToF = k => (k - 273.15) * 9/5 + 32;

  return <li>{kToF(kelvin)}</li>;
};

export default KelvinConvert;

And then passing your Kelvin temperature to it by changing

<KelvinConvert />  // Trying to add converted temperature here

To

<KelvinConvert kelvin={openWeather.kelvin} />

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 your KelvinConvert component to convert.

Also, I wrote all of this on my phone, so please forgive any typos or bad code

Thread Thread
mitchelln11 profile image
mitchelln11 Author

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 as

openWeather.main && openWeather.main.temp
Thread Thread
taylorbeeston profile image
Taylor Beeston

Weird. 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! Try

openWeather?.main?.temp

And see if that works!

As for the float formatting, use the good ol' toFixed method like so

kToF(kelvin).toFixed(2)
Thread Thread
mitchelln11 profile image
mitchelln11 Author

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!

Thread Thread
anfo000 profile image
anfo000

Wow.. I didnt know about the chaining operator.
Thanks

Thread Thread
taylorbeeston profile image
Taylor Beeston • Edited on

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