https://www.youtube.com/watch?v=p5ZP4xzhRxk&list=PLZKTXPmaJk8J_fHAzPLH8CJ_HO_M33e7-&index=7
export default function Hello() {
let name = "Mike";
function changeName() {
name = name === "Mike" ? "Jane" : "Mike";
//name์ด Mike์ผ๋๋ Jane์ผ๋ก, Jane์ผ๋๋ Mike๋ก ๋ฐ๊ฟ์ค
console.log(name);
}
return (
<div>
<h1>state</h1>
<h2>{name}</h2>
<button onClick={changeName}>Change</button>
</div>
);
}
์์ ์ฝ๋๋๋ก ์์ฑํ๋ฉด ๋ฒํผ์ ์๋ฌด๋ฆฌ ํด๋ฆญํด๋ ํ๋ฉด์ ์
๋ฐ์ดํธ ๋์ง ์๋๋ค.
๊ทธ๋์ ์๋์ฒ๋ผ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
export default function Hello() {
let name = "Mike";
function changeName() {
name = name === "Mike" ? "Jane" : "Mike";
//name์ด Mike์ผ๋๋ Jane์ผ๋ก, Jane์ผ๋๋ Mike๋ก ๋ฐ๊ฟ์ค
console.log(name);
document.getElementById("name").innerText = name; //๐ ์ถ๊ฐ
}
return (
<div>
<h1>state</h1>
<h2 id="name">{name}</h2> //๐ id ์ถ๊ฐ
<button onClick={changeName}>Change</button>
</div>
);
}
let = name
์ ๋ณ์์ผ ๋ฟ ์ปดํฌ๋ํธ๊ฐ ๊ด๋ฆฌํ๊ณ ์๋ ์ํ๊ฐ, state๊ฐ ์๋๋ค.
๊ทธ๋์ ๊ฐ์ด ๋ฐ๋์ด๋ React์์๋ ์ธ์งํ์ง ๋ชปํ๊ณ UI๋ฅผ ์
๋ฐ์ดํธํ์ง ์๋๋ค.
๊ทธ๋ผ ์ด๋ป๊ฒํด์ผ ํ ๊น?๐ค
useState
์ฌ์ฉuseState
๋ React Hook์ด๋ค.
React Hook ์ข
๋ฅ๋ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์๋๋ฐ, useState
๋ ๊ทธ ์ค ํ๋๋ค.
1) import
import { useState } from "react";
2) useState() ์ฌ์ฉ ์ฝ๋ ์์ฑ
const [name, setName] = useState();
useState
๋ ๋ฐฐ์ด([name, setName]
)์ ๋ฐํํ๋ค.
๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํ ์ฝ๋๋ก,
๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ๊ฐ [name]
์ state์ด๋ฉฐ(๋ณ์๋ช
์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค)
๋ ๋ฒ์งธ ๊ฐ [setName]
์ state๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋ ํจ์๋ค.
๋ ๋ฒ์งธ ๊ฐ [setName]
ํจ์์ ๋ด์ฉ์ด ๋ฐ๋๋ฉด React๋ ํด๋น ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ ๋๋ง ํด์ค๋ค.
3) useState() ๊ดํธ ์์ ์ด๊ธฐ ๊ฐ ์์ฑ
const [name, setName] = useState("์ด๊ธฐ๊ฐ");
1)~3)๋ฒ๊น์ง ์ ์ฉํ ์ฝ๋
import { useState } from "react";
export default function Hello() {
const [name, setName] = useState("Mike");
function changeName() {
const newName = name === "Mike" ? "Jane" : "Mike";
//name์ด Mike์ผ๋๋ Jane์ผ๋ก, Jane์ผ๋๋ Mike๋ก ๋ฐ๊ฟ์ค
setName(newName);
}
return (
<div>
<h1>state</h1>
<h2 id="name">{name}<;/h2>
<button onClick={changeName}>Change</button>
</div>
);
}
๋ฆฌํฉํ ๋งํ ์ฝ๋
import { useState } from "react";
export default function Hello() {
const [name, setName] = useState("Mike");
function changeName() {
setName(name === "Mike" ? "Jane" : "Mike"); //๐ ๋ณ๊ฒฝ
}
return (
<div>
<h1>state</h1>
<h2 id="name">{name}</h2>
<button onClick={changeName}>Change</button>
</div>
);
}
๋ฆฌํฉํ ๋งํ ์ฝ๋
import { useState } from "react";
export default function Hello() {
const [name, setName] = useState("Mike");
return (
<div>
<h1>state</h1>
<h2 id="name">{name}<;/h2>
<button
onClick={() => {
setName(name === "Mike" ? "Jane" : "Mike"); //๐ ๋ณ๊ฒฝ
}}
>
Change
</button>
</div>
);
}
์์์ ์์ฑํ Hello.js ์ปดํฌ๋ํธ๋ฅผ App.js์์ ์ธ ๋ฒ ์์ฑํ๋ฉด?
import "./App.css";
import Hello from "./component/Hello";
import Welcome from "./component/Welcome";
function App() {
return (
<div className="App">
<Hello /> //๐ Hello ์ปดํฌ๋ํธ ์ธ ๋ฒ
<Hello /> //๐
<Hello /> //๐
</div>
);
}
export default App;
ํ๋ฉด์๋ ์๋์ ๊ฐ์ด ๋์จ๋ค.
์ฌ๊ธฐ์ ์ฃผ๋ชฉํด์ผ ํ ์ ์
Change ๋ฒํผ์ ๋๋ฌ๋ Mike, Jane = state๊ฐ ์ ์ฒด๊ฐ ๋ฐ๋์ง ์๋๋ค๋ ์ ์ด๋ค.
function App() {
return (
<div className="App">
<Hello /> //๐ ์ฒซ์งธ
<Hello /> //๐ ๋์งธ
<Hello /> //๐ ์
์งธ
</div>
);
}
ํธ์์ ์ฒซ์งธ~์
์งธ<Hello/>
๋ผ๊ณ ๋ถ๋ฌ์ ์ค๋ช
ํ์๋ฉด
์ฒซ์งธ<Hello/>
์ธ ๋งจ ์ Change๋ฒํผ์ ๋๋ฅด๋ฉด ์ฒซ์งธ<Hello/>
์ name๋ง ๋ฐ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ๋์งธ<Hello/>
์ธ ์ค๊ฐ Change๋ฒํผ์ ๋๋ฌ๋ ๋์งธ<Hello/>
์ธ ์ค๊ฐ name๋ง ๋ฐ๋๋ค.
์ด๋ ๊ฒ ๋ฐ๋ก๋ฐ๋ก ์ํ๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค๋ ์ !