코드 설명을 해보자면
화면 높이의 100% vh
반복되는 사진 없앨려면 no repeat
반복되는 사진은 없앴는데 짤린 부분이 보기싫으면 cover까지 해주면됨
이제 날씨 박스 만들기
- weatherBox라는 컴포넌트 만들어서 생성해주자
이 정도로 일단 해두고
버튼도 만들어 줄건데 이것도 컴포넌트로 만들지만 디자인 하기 귀찮아서 react의 bootstrap이란걸 사용해볼거다.
버튼을 생성후에 가운데 정렬을 하려고 했는데 한가지 문제가 발생했다.
<div class="container">
<WeatherBox/>
<WeatherButton/>
</div>
display:flex때문에
flexable하게 아이템 조절에는 너무 좋지만 div안에있는 모든 아이템들을 가로 정렬로 바꿔버린다.
flex의 방향을 바꿔줘야 하는데 flex-column으로 변경해주자
날씨 데이터 보여주기
weather date는 ui에 보여지는 데이터
그래서 특히나 그걸 state에 담아준다.
state에 담아주기 위해서 app으로 돌아간다.
const [weather,setWeather]=useState(null)
// weather데이터는 보여지는 데이터라서 일단 state에 담아준다.
const getWeatherByCurrentLocation = async(lat,lon)=>{
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=0b99d295cfa7cbbaa3cdc197e28cacc4`;
let response = await fetch(url)
let data = await response.json();
setWeather(data);
}
// 데이터가 왔을때 weather의 값을 넣어주어야 한다.
바로 Destructuring
import React from 'react'
const WeatherBox = ({weather}) => {
console.log("weather?",weather)
return (
<div className="weather-box">
<div>{weather.name}</div>
<h2>30도 / 230화씨</h2>
<h3>맑은 하늘</h3>
</div>
)
}
export default WeatherBox;
<div>{weather?.name}</div>
&&랑 같은 역할인
삼항연산식 이 방법으로도 해결 가능하다. weather이 있어? 그러면 name을 보여줘
이제 paris버튼을 누르면 paris의 온도랑 화씨 온도 날씨 상태 이런정보를 가져오게 해보자 api만 가져오면 됨
'react' 카테고리의 다른 글
코알누-react의 lifecycle을 활용한 날씨앱 만들기-4(도시별 날씨 가져오기) (0) | 2023.08.01 |
---|---|
코알누-react의 lifecycle을 활용한 날씨앱 만들기-3(버튼 보여주기) (0) | 2023.07.31 |
코알누-react의 lifecycle을 활용한 날씨앱 만들기 (0) | 2023.07.31 |
코알누-리액트 lifecycle (0) | 2023.07.31 |
코알누 클래스 컴포넌트(옛날 방식이지만 중요한것) (0) | 2023.07.29 |