Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

react fetch data (weather)

interface WeatherDisplayProps {
  location: string;
}
const WeatherDisplay: FC<WeatherDisplayProps> = ({ location }) => {
  const [weather, setWeather] = useState<number>();
  useEffect(() => {
    const fetchWeather = async () => {
      const response = await WeatherAPI.getWeather(location);
      setWeather(response.celsius);
    };
    fetchWeather();
  }, [location]);
return (
    <div>
      <h1>Weather</h1>
      <p>
        Today weather in {location} is {weather} celsius{" "}
      </p>
    </div>
  );
};
export default WeatherDisplay;
Source by levelup.gitconnected.com #
 
PREVIOUS NEXT
Tagged: #react #fetch #data
ADD COMMENT
Topic
Name
4+8 =