๐๋ ์จ ์์ ฏ ๋ฐ๋ก๊ฐ๊ธฐ
- searchForm ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด์, ๊ฒ์์ฐฝ์ ๋ง๋ ๋ค.
- ๊ฒ์form์ ์ ์ถํ๋ฉด, ํด๋น ์ง์ญ์ ๋ ์จ๋ฅผ ๋ณด์ฌ์ฃผ๋ ํ์ด์ง๋ก ์ด๋ํ๋ค.
- ํ์ด์ง ์ด๋์ useNavigate Hook์ ์ฌ์ฉํ๋ค.
- setInputLocation ํจ์๋ฅผ ํธ์ถํ์ฌ, preInput ๊ฐ์ ๋๊ฒจ์ค๋ค.
const navigate = useNavigate();
const submitForm = (event) => {
event.preventDefault();
setInputLocation(preInput);
setPreInput('');
navigate('/');
};
<SearchForm>
<h2>Local search</h2>
<form onSubmit={submitForm}>
<input type='text' placeholder='Seoul' value={preInput} onChange={(event) => setPreInput(event.target.value)} />
<button type='submit'>๐</button>
</form>
</SearchForm>;
Promise.all
๋ก ํ์ฌ ๋ ์จ์ ์ผ๊ธฐ์๋ณด API๋ฅผ ํธ์ถํ์ฌ, ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์จ๋ค.- ๊ฐ๊ฐ API์์ ์ถ์ถํ ๋ฐ์ดํฐ๋ฅผ state์ ์ ์ฅํ๋ค.
- ์ฒซ ์ ์ ์ ๋ก๋ฉ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ณ ํ์ฌ ์์น ๋ฐ์ดํฐ ๋ ธ์ถํ๋ค.
// ํ์ฌ ์์น๋ฅผ ๋ฐ์์ค๋ ํจ์
function currentLocation() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
resolve({ latitude, longitude });
},
(error) => {
reject('error');
}
);
});
}
// date ๊ฐ์ฒด๋ฅผ ์ด์ฉํด ๋ฐ์ดํฐ ์ถ์ถ
const oneDay = 1000 * 60 * 60 * 24;
const offset = 1000 * 60 * 60 * 9;
const current = new Date().getTime() + offset;
const DesiredTime = ' 21:00:00';
const oneDaysLater = new Date(current + oneDay).toISOString().slice(0, 10) + DesiredTime;
const twoDaysLater = new Date(current + oneDay * 2).toISOString().slice(0, 10) + DesiredTime;
const threeDaysLater = new Date(current + oneDay * 3).toISOString().slice(0, 10) + DesiredTime;
// 3์ผ์น ๋ฐ์ดํฐ๋ง ์ถ์ถ (UTC ์๊ฐ ๊ณ์ฐ ๊ณ ๋ ค)
const weatherData = resultForecast.list.filter((item) => {
return item.dt_txt === oneDaysLater || item.dt_txt === twoDaysLater || item.dt_txt === threeDaysLater;
});
- intl.DateTimeFormat์ ์ด์ฉํด์ ํ์ฌ ์๊ฐ์ ๋ฐ์์จ๋ค.
- styled-components์ props๋ฅผ ์ด์ฉํด์, ์๊ฐ๋๋ณ๋ก ๋ฐฐ๊ฒฝํ๋ฉด์ ๋ณ๊ฒฝํ๋ค.
// ํ์ฌ ์๊ฐ์ ๋ฐ์์์ props๋ก ์ ๋ฌ
const currentTime = new Intl.DateTimeFormat(
'en', { hour: '2-digit', hourCycle: 'h23' }).format(new Date()
)
const GlobalStyle = createGlobalStyle`
body {
padding: clamp(1,5rem, 5vw, 3rem);
background: ${(props) => (
props.times > 6 && props.times <= 17
? 'linear-gradient(345.21deg, #89F7FE 2.6%, #66A6FF 85.16%)'
: props.times > 17 && props.times <= 19
? 'linear-gradient(180deg, #3F51B1 -7.36%, #5A55AE 5.68%, #7B5FAC 17.73%, #8F6AAE 30.77%, #A86AA4 42.81%, #CC6B8E 54.86%, #F18271 67.9%, #F3A469 79.94%, #F7C978 92.99%)'
: 'linear-gradient(167.44deg, #08244F 0%, #134CB5 47.38%, #0B42AB 100%)'
)};
}