Props, State์ ํน์ง๊ณผ ์ด๋ฅผ ํ์ฉํ ์ด๋ฒคํธ ์ฒ๋ฆฌ์ ๋ํด ํ์ตํ๋ค.
<parent>
,<child>
์ปดํฌ๋ํธ๋ฅผ ์ ์ธํ๋ค.
<parent>
์ปดํฌ๋ํธ ์์<child>
์ปดํฌ๋ํธ๋ฅผ ์์ฑํ๋ค.
function Parent(){
return(
<div className="parent">
<Child />
</div>
)
}
funtion Child(){
return(
<div className="child"></div>
)
}
{,}
์ฌ์ฉ<Child attribute={value} />
<Child text={"..."} />
<Parent>
์ปดํฌ๋ํธ์ ์ ๋ฌfunction Child(props){
return(
<div className="child"></div>
)
}
function Child(props){
return(
<div className="child">
<p>{props.text}</p>
</div>
)
}
props.children
์ฌ์ฉ ์ ํด๋น value์ ์ ๊ธํด ์ฌ์ฉํ ์ ์๋ค.function Parent(){
return(
<div className="parent">
<Child>I'm Child</Child>
</div>
)
}
function Child(props){
return(
<div className="child">
<p>{props.children}</p>
</div>
)
}
import { userState } from "react"
const [state์ ์ ์ฅํ ๋ณ์, ํ์ฌ state๋ฅผ ๊ฐฑ์ ํ ๋ณ์] = useState(state์ด๊ธฐ ๊ฐ ํ ๋น)
function CheckboxExample(){
const[isCheckde, setIsCheckde] = useState(false)
}
const stateHookArray = useState(false);
const isChecked = stateHookArray[0];
const setIsChecked = stateHookArray[1];
states๋ ๊ฐ ๋ณ๊ฒฝ์ ํจ์ ํธ์ถ๋ก ๋ณ๊ฒฝํด์ผํ๋ค.(๊ฐ์ x)
- ๊ฐ์ ๋ก ๋ณ๊ฒฝํ ๊ฒฝ์ฐ ๋ฆฌ๋ ๋๋งx, state๊ฐ ์ ๋๋ก ์๋ ์๋ ์ ์๋ค.
์์ฃผ ๋ณ๊ฒฝ๋๋ ์ค์ํ ๋ฐ์ดํฐ๋ ๋ณ์ ๋์ state๋ก ์ ์ฅํ๋ค.
<button onClick={hadleEvent}>...</button>
{hadleEvent()}์ฒ๋ผ
()
๊ฐ ์์ ๊ฒฝ์ฐ ํด๋น ํจ์๋ฅผ ๋ฐ๋ก ์คํํ๋ค๋ ๋ป์ด๋ค.
function NameForm(){
const[name, setName] = useSate("");
const handleChange = (event) => {
setName(event.target.value);
}
}
return(
<input type="text" value={name} onChange={handleChange}></input>
<h1>{name}</h1>
)
return(
<button onClick={() => alert(name)}></button>
)
{/*ํจ์ ์์ฒด๋ฅผ ์ ๋ฌํ๋ค. */}
const handleClick = () => {
alert(name);
}
return(
<button onClick={handleClick}>
)
props์ state์ ์ฐจ์ด์ ์ ์๊ฒ๋์์ง๋ง
์์ง๋ ์ฌ์ฉ ๋ฐฉ๋ฒ์ด ์ดํด ์ ๋๋ ๋ถ๋ถ์ด ๋ช๊ฐ์ง ์กด์ฌํ๋ค.
์ดํด ์ ๋๋ ๋ถ๋ถ์ ๋ง์ด ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ ๋ฐ์ ์์ง ์์๊น ์ถ๋ค.
๋ํ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ Javascript์ ๋น์ทํ๋ฉด์ ๋ง์ด ๋ค๋ฅธ ๊ฑฐ ๊ฐ๋ค.
{ํจ์()}
, {ํจ์}
์ด ๋๊ฐ์ง ์ฐจ์ด์ ์ ์ ์๊ณ ์์ฑํด์ผํ ๊ฑฐ ๊ฐ๋ค.