๐โโ๏ธ state, props์ ๊ฐ๋ ์ ๋ํด์ ์ดํดํ๊ณ , ์ค์ ํ๋ก์ ํธ์ ๋ฐ๋ฅด๊ฒ ์ ์ฉํ ์ ์๋ค.
๐โโ๏ธ React ํจ์ ์ปดํฌ๋ํธ(React Function Component)์์ state hook์ ์ด์ฉํ์ฌ state๋ฅผ ์ ์ ๋ฐ ๋ณ๊ฒฝํ ์ ์๋ค.
๐โโ๏ธ React ์ปดํฌ๋ํธ(React Component)์ props๋ฅผ ์ ๋ฌํ ์ ์๋ค.
๐โโ๏ธ ์ด๋ฒคํธ ํธ๋ค๋ฌ ํจ์๋ฅผ ๋ง๋ค๊ณ React์์ ์ด์ฉํ ์ ์๋ค.
๐โโ๏ธ ์ค์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ปดํฌ๋ํธ๋ฅผ ๋ณด๊ณ ์ด๋ค ๋ฐ์ดํฐ๊ฐ state์ด๊ณ props์ ์ ํฉํ์ง ํ๋จํ ์ ์๋ค.
๐โโ๏ธ ์ค์ ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ ์ ์ ํฉํ state์ props์ ์์น๋ฅผ ์ค์ค๋ก ์ ํ ์ ์๋ค.
๐โโ๏ธ React์ ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ(One-way data flow)์ ๋ํด ์์ ์ ์ธ์ด๋ก ์ค๋ช ํ ์ ์๋ค.
๋ฆฌ์กํธ์ ์ ๋ฌ ์ฃผ์ฒด๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ์ด๋ค. ๋ฐ์ดํฐ ํ๋ฆ์ด top-down์ธ ํํฅ์์ด๋ค.
usestate๋ ๊ตฌ์กฐ๋ถํดํ ๋น์ ํ์ฉํ๊ณ ์์๋ค.
input tag๋ ๊ธ์จ๊ฐ textContent๊ฐ ์๋๋ผ value๋ค.
let arr = ["Bora","Lee"]
let [firstName,surname] = arr
console.log(firstName) // "Bora"
console.log(surname) // "Lee"
let options = {
title : "Menu",
width : 100,
height : 200
}
let{title,width,height} = options
console.log(title) // "Menu"
console.log(width) // 100
console.log(height) // 200
<input type = "button" onclick ="alert('๋ฒํผ ํด๋ฆญ๋จ!')" ></input>
onclick ์์ฑ์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์ง์ ์์ฑํ๊ฑฐ๋์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ์ํด ๊ตฌํํ ํจ์๋ฅผ ํธ์ถํฉ๋๋ค. onXXX ๋ฅผ ์ด์ฉํด ์ฌ๋ฌ ์ด๋ฒคํธ ํธ๋ค๋ฌ ์์ฑ์ ๋ถ์ฌํ๋ ๊ฒ๋ ๊ฐ๋ฅ ํฉ๋๋ค.
<script>
document.getElementById('b1').onclick = function() {alert("๋ฒํผ ํด๋ฆญ๋จ!!")}
</script>
<body>
<input type="button" id="b1">
</body>
document.getElementById() ๋ฉ์๋๋ HTML๋ฌธ์์์ ์ง์ ํ id ์์ฑ์ ์ฐพ์ ๋ฐ์ธ๋ฉ ํฉ๋๋ค.
์ด๋ฒคํธ ๋ฐ์์ ํ ๋น๋ ํจ์๋ถ๊ฐ ํธ์ถ ๋์ด ์ฝ๋ฐฑ ํจ์ ๋ผ๊ณ ํฉ๋๋ค.
์ ํจ์ฑ๊ฒ์ฌ sprint๋ ์์ฑํ ์ด๋ฒคํธํจ์๋ค.
elInputPasswordRetype.onkeyup = function() {
passwordFlag = isMatch(elInputPassword.value,elInputPasswordRetype.value)
if(passwordFlag){
//์ผ์นํ๋ค๋ฉด
elMismatchMessage.classList.add('hide')
elInputPasswordRetype.classList.remove('underline')
}
else{
//์ผ์นํ์ง ์๋๋ค๋ฉด
elMismatchMessage.classList.remove('hide')
elInputPasswordRetype.classList.add('underline')
}
}
onkeyup = ํจ์์์ฒด๋ฅผ ๋ฃ์ด์คฌ๋ค. ๋ง์ฝ ํจ์๋ฅผ ํํ์์ผ๋ก ์ผ๋๋ผ๋ฉด
onkeyup = ํจ์์ด๋ฆ ์ด๋ ๊ฒ ์์ฑํ์ ๊ฒ์ด๋ค.
ํํ์์์ฐ๋ ๋๋ฒ์งธ์ฒ๋ผ ์ ์ธ๋ฌธ์ผ๋ก ์ฐ๋ ์ด์จ๋ ํจ์()๊ฐ ์๋๋ผ ํจ์ ๊ทธ ์์ฒด๋ฅผ ๋ฃ์ด์ค๊ฒ์ด๋ค.
๊ทผ๋ฐ html์์๋
<button>Event</button>
์ด๋ ๊ฒ ์ผ์ ๊ฒ์ด๋ผ๊ณ ์์ฑ๋์ด์๋ค.
๋ฌผ๋ก ์ ํจ์ฑ๊ฒ์ฌ sprint์ ์ฝ๋๋ jsํ์ผ์ ์์ฑํ๊ฑฐ๋ค. queryselector๋ฅผ ์ฌ์ฉํด์ ๋ณ์๋ฅผ ์ด์ฉํด์ ์ด๋ฒคํธ๋ฅผ jsํ์ผ์ ๋ง๋ ๊ฑฐ๋ค.
htmlํ์ผ์์ onclick์ ์ฃผ๋ ค๋ฉด jsํ์ผ์ ํจ์๋ฅผ ์คํํด์ผํด์ ()๋ฅผ ๋ถ์ธ ๊ฒ์ด๋ค.
const [name,setName] = useState("")
useState์ ์ด๊ธฐ๊ฐ์ 2๊ฐ๋ก ์ค์ ํ๋ฉด ์ค๋ฅ๊ฐ ์๋๊ณ undefined๊ฐ ๋์จ๋ค๊ณ ํ๋ค ใ
ใทใ
ใท
useState๋ฅผ ์ฌ๋ฌ๋ฒ ์ฐ๊ธฐ ๊ท์ฐฎ๋ค๋ฉด ์ด๊ธฐ๊ฐ์๋ฆฌ์ ๊ฐ์ฒด๋ก ์ฌ๋ฌ ์์ฑ๋ค์ ์ ์ ์ ์๋ค. useState์ ์ต์ํด์ง๋ฉด ๊ทธ ๋ ์ฌ์ฉํด๋ณด์.
๋ถ๋ชจ์ปดํฌ๋ํธ์์ ๋ณ์์ ๋ฃ์๊ฑด ๋ค ์์์ปดํฌ๋ํธ์ props๋ก ์ ๋ฌํด ์ค ์ ์๋ค.
state๋ props๋ฅผ ์ง์ ๊ณ์ฐํ ์ ์๋ค๋ฉด ๊ทธ๊ฒ์ state๊ฐ ์๋๋ค.
-> ๋ง์ฝ state์ props๋ฅผ ํตํด์ ๊ณ์ฐ์ด ๊ฐ๋ฅํ๋ค๋ฉด ๊ตณ์ด ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ํ(state)๋ก ๊ด๋ฆฌํ ํ์๊ฐ ์๋ค.
map ๋ฉ์๋๋ง ์์๋๋ฐ Map์ด๋ผ๋๊ฒ์ด key์ value๋ฅผ ๋ง๋ค๊ฒํ๋ค๋๊ฒ์ ์์๋ค. ์ ๊ธฐํ๋ค ใ ใ