loading...

re: Run Function after useEffect to Manipulate Data? VIEW POST

FULL DISCUSSION
 

You can do everything in the effect function:

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import {KelvinConvert} from './MathConversions';

const LocalWeather = () => {
    const [openWeather, setWeather] = useState({});

    useEffect(() => {
        axios.get(`https://api.openweathermap.org/data/2.5/weather? 
        q=London,uk&APPID=${process.env.REACT_APP_WEATHER_KEY}`)
        .then(data => {
            setWeather( KelvinConvert(data.data));
         });
    }, []); //  Run once on load

    return (
        <ul id="weather-container">
            <KelvinConvert />  // Trying to add converted temperature here
            <li>{openWeather.main && openWeather.main.temp}&deg;F</li>
            <li>{openWeather.weather && openWeather.weather[0].main}</li>
            <li>{openWeather.name}</li>
        </ul>
    );
}

export default LocalWeather;

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.

Code of Conduct Report abuse