Code States
Front-end boost camp
Today
I
Learned
๐ ์์ํ ! 5์ 22์ผ, 29์ผ์ฐจ ์์
โผ ์ปดํฌ๋ํธ ์ ์ธ
function Parent() {
return (
<div className="parent">
<h1>I'm the parent</h1>
<Child />
</div>
);
};
function Child() {
return (
<div className="child"></div>
);
};
โผ HTML ์์ฑ๊ณผ ๊ฐ ๋ค๋ฃจ๋ ๋ฒ
<a href="www.codestates.com">Click me to visit Code States</a>
โผ React์์ JSX ์์ฑ ๋ฐ ๊ฐ์ ํ ๋นํ๋ ๋ฐฉ๋ฒ1
<Child attribute={value} />
โผ React์์ JSX ์์ฑ ๋ฐ ๊ฐ์ ํ ๋นํ๋ ๋ฐฉ๋ฒ2
<Child text={"I'm the eldest child"} />
โผ Child ์ปดํฌ๋ํธ์์ props ๋งค๊ฐ๋ณ์ ์ฌ์ฉ ์์
function Child(props) {
return (
<div className="child"></div>
);
};
โผ Child ์ปดํฌ๋ํธ์์ props.text ๋ ๋๋ง ์์
function Child(props) {
return (
<div className="child">
<p>{props.text}</p>
</div>
);
};
// useState ๋ฌธ๋ฒ์์
function CheckboxExample() {
// ์๋ก์ด state ๋ณ์๋ฅผ ์ ์ธํ๊ณ , ์ฌ๊ธฐ์๋ ์ด๊ฒ์ isChecked๋ผ ๋ถ๋ฅด๊ฒ ์ต๋๋ค.
const [isChecked, setIsChecked] = useState(false);
}
// useState ๊ตฌ์กฐ ๋ถํด ํ ๋น ์์
function CheckboxExample() {
// 1๋ฒ ์ฝ๋๋ฅผ ํ์ด์ฐ๋ฉด
const [isChecked, setIsChecked] = useState(false); // 1๋ฒ
//...
// 2๋ฒ ์ฝ๋์ ๊ฐ์ต๋๋ค.
const stateHookArray = useState(false); // 2๋ฒ
const isChecked = stateHookArray[0];
const setIsChecked = stateHookArray[1];
}
const [state ์ ์ฅ ๋ณ์, state ๊ฐฑ์ ํจ์] = useState(์ํ ์ด๊ธฐ ๊ฐ);
function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
// const [state ์ ์ฅ ๋ณ์, state ๊ฐฑ์ ํจ์] = useState(state ์ด๊น๊ฐ);
<span>{isChecked ? "Checked!!" : "Unchecked"}</span>
function NameForm() {
const [name, setName] = useState("");
const handleChange = (e) => {
setName(e.target.value);
}
return (
<div>
<input type="text" value={name} onChange={handleChange}></input>
<h1>{name}</h1>
</div>
)
};
function NameForm() {
const [name, setName] = useState("");
const handleChange = (e) => {
setName(e.target.value);
}
return (
<div>
<input type="text" value={name} onChange={handleChange}></input>
<button onClick={alert(name)}>Button</button>
<h1>{name}</h1>
</div>
);
};
-- onClick ์ด๋ฒคํธ์ ํจ์๋ฅผ ์ ๋ฌํ ๋๋ ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ด ์๋๋ผ ๋ฆฌํด๋ฌธ ์์์ ํจ์๋ฅผ ์ ์ํ๊ฑฐ๋ ๋ฆฌํด๋ฌธ ์ธ๋ถ์์ ํจ์๋ฅผ ์ ์ํ ํ ์ด๋ฒคํธ์ ํจ์ ์์ฒด๋ฅผ ์ ๋ฌํด์ผ ํจ
// ํจ์ ์ ์ํ๊ธฐ
return (
<div>
...
<button onClick={() => alert(name)}>Button</button>
...
</div>
);
};
// ํจ์ ์์ฒด๋ฅผ ์ ๋ฌํ๊ธฐ
const handleClick = () => {
alert(name);
};
return (
<div>
...
<button onClick={handleClick}>Button</button>
...
</div>
);
};
... ์ธ ํ์ํ ์ด๋ฒคํธ ํจ์๋ ๊ทธ๋๊ทธ๋ ์ฐพ์ ์จ์ผํ ๋ฏ
๊ฐ๋ ์ฃผ์๋จน๊ธฐ ๋ฒ ์ฐผ๋ ์ค๋ ์์ ~~ ์ ๋ ์ ๋ฆฌ์กํธ ์ง๋ ์ค์ต ๋ณต์ต + ๋ด์ผ ํ์ด ํ๋ก๊ทธ๋๋ฐ์ ์ํ ๊ณ ๋ฏผ์ ์๊ฐ์ด ํ์ง!