2025 / 04 / 01
오늘 수업 시간에는 React에서 날씨 API를 사용하는 실습을 하였습니다.
자바스크립트 배울 당시에 했던 내용이었는데 그래도 아직 헷갈리는 부분이 있었습니다.
작성한 코드를 바탕으로 최대한 이해하기 쉽게 벨로그를 작성해보겠습니다.
- React로 OpenWeatherMap API를 사용해 현재 날씨 정보를 가져오는 코드입니다.
- 컴포넌트 별로 코드 흐름을 세세하게 정리해보도록 하겠습니다.
App 컴포넌트
- 메인 컴포넌트로, 사용자의 위치를 기반으로 날씨 정보를 가져오는 역할을 합니다.
- useEffect, useState는 React에서 상태 관리와 사이드 이펙트를 처리하는 주요 훅입니다.
import { useEffect, useState } from "react"; import "./App.css"; import Weather from "./weather";
useState
useEffect
- 컴포넌트에 있는 값들의 상태를 관리합니다.
const [latitude, setLatitude] = useState(null); const [longitude, setLongitude] = useState(null); const [weather, setWeather] = useState(null);
latitude, longitude
weather
- 사용자의 위도와 경도 위치를 받아올 수 있습니다.
useEffect(() => { const getLocation = () => { navigator.geolocation.getCurrentPosition( (position) => { const crd = position.coords; console.log("현재 위치는?"); console.log(`Latitude : ${crd.latitude}`); console.log(`Longitude: ${crd.longitude}`); setLatitude(crd.latitude); setLongitude(crd.longitude); }, (error) => { console.log(error); } ); }; getLocation(); }, []);
navigator.geolocation.getCurrentPosition
useEffect
- 받아온 위치 값을 기준으로 해당 위치의 날씨 데이터를 받아올 수 있습니다.
useEffect(() => { const fetchWeather = async () => { if (latitude && longitude) { const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${your_API_key}&units=metric`; try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); console.log(data); setWeather(data); } catch (error) { console.log(error); } } }; if (latitude && longitude) { fetchWeather(); } }, [latitude, longitude]);
fetchWeather
fetch
- 위치가 아직 받아지지 않았다면 로딩중 메시지를 표시합니다.
- 위치 정보가 있으면 Weather 컴포넌트를 렌더링하여 날씨 데이터를 전달합니다.
if (latitude === null || longitude === null) { return <p>로딩중..</p>; } return <Weather weather={weather} />;
Weather 컴포넌트
- weather 데이터를 props로 받아와서 화면에 표시합니다.
export default function Weather({ weather }) { return ( <Container> {weather && ( <Innerbox> <h3>지금 날씨는?</h3> <img src={`http://openweathermap.org/img/wn/${weather.weather[0].icon}.png`} alt={weather.weather[0].description} /> <Infobox> <p>온도 : {weather.main.temp.toFixed(0)} °C</p> <p>날씨 : {weather.weather[0].description}</p> <p>위치 : {weather.name}</p> </Infobox> </Innerbox> )} </Container> ); }
weather.weather[0].icon
weather.main.temp.toFixed(0)
Container부분 styled-components
- 날씨 정보를 담을 큰 박스의 스타일을 정의한 것입니다.
const Container = styled.div` border: solid 1px black; border-radius: 40px; padding: 10px; text-align: center; background-color: rgb(115, 167, 184); margin: 0 auto; margin-top: 50px; width: 400px; `;
Innerbox부분 styled-components
- 날씨 정보를 감싸는 박스입니다.
- 온도, 날씨 설명, 위치 등의 정보를 표시합니다.
const Innerbox = styled.div` border-radius: 30px; margin: 10px; padding: 30px; text-align: center; background-color: rgb(255, 255, 255); box-shadow: inset 5px 5px 10px rgba(0, 0, 0, 0.2); display: flex; justify-content: center; align-items: center; flex-direction: column; gap: 10px; `;
Infobox부분 styled-components
- 개별 날씨 정보들(온도, 설명, 위치)을 담은 박스입니다.
const Infobox = styled.div` border-radius: 20px; padding: 20px; text-align: center; margin: 0 auto; width: 100%; transition: transform 0.3s ease, background-color 0.3s ease; &:hover { transform: translateY(-5px); box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); background-color: white; } & p { margin: 5px; } `;
&:hover
59일차 후기
- API 데이터를 가져와 해당 데이터를 화면에 출력하는 것은 익숙해진 것 같습니다.
- 근데 이 API만 여러 번 사용해봐서.. 이것만 익숙해진거 같기도 합니다.
- styled-components도 계속 사용하니까 어색한 부분은 없어진 것 같습니다.
- useEffect 사용하는 것도 이해하고 사용하는건지, 그냥 다른 코드 참고하면서 작성해서 괜찮은건지 모르겠어서 프로젝트에서 계속 사용해보면서 확인해야할 것 같습니다.
- API 사용을 위해 데이터를 받아오는 fetch의 복습이 필요할 것 같습니다. (˶ᐢ. .ᐢ˵)