ํฌ์คํ ์์น ๋์๊ธฐ๋ฐ ๋ ์จ์ฑ(4)์ ์ด์ ๋ง๋ฌด๋ฆฌ ์์
App.js๊ฐ ๋ชจ๋ ๋ก์ง์ ๋ค ๊ฐ๊ณ ์๊ณ , ์ปดํฌ๋ํธ์ ๋ณด๋ด์ฃผ๋ ์์ผ๋ก ์์ ์ ์งํํด๋ณด๋๋ก ํ๋ค.
react one-way data flow
๋๋ฌธ์ด๋ค. ์ปดํฌ๋ํธ ํ์ ๋ผ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๊ธฐ๊ฐ ์๋นํ ์ด๋ ต๋ค.
๋ฆฌ์กํธ ํน์ง one-way reactive data flow
https://tkssharma.gitbook.io/react-training/day-01/react-js-3-principles/one-way-data-
The foundation of understanding component relationships in React and parent component hierarchy…
Props stands for properties. It is an object that passes values and data from parent to the child component. These values can be strings…
sandienunez.medium.com
์์ ๊ฐ์ด ์์ฑํ๋ React ์ปดํฌ๋ํธ ์ค state ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ฅผ Stateful ์ปดํฌ๋ํธ๋ผ ํ๊ณ , state ๋ฅผ ์ฌ์ฉํ์ง ์๋ ์ปดํฌ๋ํธ๋ฅผ Stateless ์ปดํฌ๋ํธ ๋ผ ํ๋ค. ๋ง์ฝ state๋ฅผ ์ฌ์ฉํ์ง ์๋ Stateless ์ปดํฌ๋ํธ๋ก ์์ฑํ ์ ์๋ค๋ฉด ์ต๋ํ state๋ฅผ ์ฌ์ฉํ์ง ์๋๋ก ์ปดํฌ๋ํธ๋ฅผ ๊ตฌํํด์ผ ์ดํดํ๊ธฐ ์ฝ๋ค. ์ดํดํ๊ธฐ ์ฌ์์ผ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ๊ธฐ ์ฝ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ๊ธฐ ์ฌ์์ผ ๋ฒ๊ทธ๋ ์ ์ง๋ณด์๊ฐ ์ฌ์์ง๋ค.
์์ฝ ์ ๋ฆฌ : react one-way data flow์ ๋ฐ๋ผ, ๋ถ๋ชจ์์ ์์์ผ๋ก๋ง ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์์ด
๋ฐ์ดํฐ๋ ๋ถ๋ชจ๊ฐ ๋ค๊ฒ ๋๋๋ฐ, ์ด๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ stateful, ๋ถ๋ชจ๊ฐ ์ฃผ๋ ๋ฐ์ดํฐ๋ง ๋ฐ๋ ์ปดํฌ๋ํธ๋ stateless ์ปดํฌ๋ํธ๋ก ๊ตฌ์ฑํด๋ฒ๋ฆฌ๋ฉด
state๊ฐ ์๋ stateless ์ปดํฌ๋ํธ๋ ์ฌ์ฌ์ฉ์ด ์ฌ์์ง๋ค. (๋ฐ์ดํฐ ๊ฑฑ์ ์์ด ์ ์ง๋ณด์ ํด๋๋จ. ์ค์ ๋ฐ์ดํฐ๋ ๋ชจ๋ stateful ์ปดํฌ๋ํธ์ ์์)
์ต๋ํ ๋ชจ๋ ์ปดํฌ๋ํธ๋ฅผ stateless๋ก ๋ง๋ค๊ณ ๋ช๊ฐ์ stateful์ปดํฌ๋ํธ์์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ๊ตฌ์กฐ๊ฐ ์ด์์
import './App.css';
import { useEffect, useState } from 'react';
import WeatherBox from "./component/WaetherBox";
import WeatherButton from "./component/WeatherButton";
import 'bootstrap/dist/css/bootstrap.min.css';
//1. ์ฑ์ด ์คํ๋์๋ง์ ํ์ฌ ์์น ๊ธฐ๋ฐ์ ๋ ์จ๊ฐ ๋ณด์ธ๋ค.
//2. ๋ ์จ ์ ๋ณด์๋ ๋์, ์ญ์จ, ํ์จ ๋ ์จ ์ํ
//3. 5๊ฐ์ ๋ฒํผ์ด ์๋ค (1๊ฐ๋ ํ์ฌ ์์น, 4๊ฐ๋ ๋ค๋ฅธ ๋์)
//4. ๋ก๋ฉ ์คํผ๋ ๊ธฐ๋ฅ ์ถ๊ฐ
function App() {
const [userWeather, setUserWeather] = useState(null);
const [city, setCity] = useEffect('');
const cities = ['Your Weather', 'NewYork', 'Toronto', 'Paris', 'Rondon'];
const getCurrentLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
getWeatherByCurrentLocation(latitude, longitude);
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
//await ์ฐ๊ณ ์ ํ๋ ํจ์๋ async ์ฌ์ผ ํ๋ค!
const getWeatherByCurrentLocation = async (lat, lon) => {
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=b13282392fd34dd9436686c7c7ecda83&units=metric`;
let response = await fetch(url) //๋น๋๊ธฐ(์ง๋
: url์ patchํ๋ ๊ฒ์ ๊ธฐ๋ค๋ ค๋ฌ๋ผ!) : url ๋ก๋ฉํ ๋ค ์๋ต๊ฐ์ ๋ฐ๊ธฐ๋ก ํจ
let data = await response.json(); //์๋ต์์ json ์ถ์ถํ๋ ๊ฒ์ ๊ธฐ๋ค๋ ค๋ฌ๋ผ
setUserWeather(data);
console.log(data);
}
useEffect(() => {
getCurrentLocation()
}, []);
return (
<div>
<div className='main'>
<WeatherBox weather={userWeather} />
<WeatherButton cities={cities} setCity={setCity} />
</div>
<div>
</div>
</div >
);
}
export default App;
<WeatherButton cities={cities} setCity={setCity} />
setCity๋ ํจ์์ด๋ค. ์ด๋ฐ ์์ผ๋ก ํจ์๋ ๋ณด๋ด์ค ์๊ฐ ์๋ค.
useEffect(() => {
console.log('city', city);
}, [city]);
๋ฆฌ์กํธ ๊ณต์๋ฌธ์-useEffect
ํ
React์ class ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋์ ์น์ํ๋ค๋ฉด, useEffect Hook์ componentDidMount์ componentDidUpdate, componentWillUnmount๊ฐ ํฉ์ณ์ง ๊ฒ์ผ๋ก ์๊ฐํด๋ ์ข์ต๋๋ค.
๋ค์ ํฌ์คํ ์์๋ ๋ก๋ฉ์คํผ๋๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ์ผ๋ก ๋ ์จ์น ๋ง๋ค๊ธฐ๋ฅผ ๋ง๋ฌด๋ฆฌํ๊ฒ ๋ค.