๐
๊ฐ์ ์ ์ฅํ๊ณํ์ฉํ๋ ๋ฐฉ๋ฒ ์ค ํ๋ ( useState ๋ ๋น์ท? )
Re-rendering๊ณผ ์๊ด์์ด ๊ฐ์ ๊ธฐ์ตํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๊ฒ
๐จ ์๋ฐ์คํฌ๋ฆฝํธ DOM API๋ฅผ ์ง์ ์ฌ์ฉํ์ง ์๊ณ DOM ์์๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํ ์ฉ๋๋ก ์์ฃผ ์ฌ์ฉ
import "./App.css";
import { useRef } from "react";
function App() {
const ref = useRef("์ด๊ธฐ๊ฐ");
console.log("ref", ref);
return (
<div>
<p>useRef์ ๋ํ ์ด์ผ๊ธฐ์์.</p>
</div>
);
}
export default App;

๋ณ๊ฒฝ๋ ๊ฐ๋ฅ
import "./App.css";
import { useRef } from "react";
function App() {
const ref = useRef("์ด๊ธฐ๊ฐ");
console.log("ref 1", ref);
ref.current = "๋ฐ๊พผ ๊ฐ";
console.log("ref 1", ref);
return (
<div>
<p>useRef์ ๋ํ ์ด์ผ๊ธฐ์์.</p>
</div>
);
}
export default App;

๐ก ์ด๋ ๊ฒ ์ค์ ๋
ref ๊ฐ์ ์ปดํฌ๋ํธ๊ฐ ๊ณ์ํด์ ๋ ๋๋ง ๋์ด๋ unmount ์ ๊น์ง ๊ฐ์ ์ ์งํฉ๋๋ค! (์ค์)
์ด๋ฌํ ํน์ง๋ค ๋๋ฌธ์ useRef๋ ๋ค์ ์ฉ๋๋ก ์ฌ์ฉ๋๋ค.
state์ ๋น์ทํ ์ญํ ์ ํ์ง๋ง ๋ณํ๊ฐ ์ผ์ด๋๋ฉด ๋๋๋ง์ด ์ผ์ด๋๋ state์ ๋ฌ๋ฆฌ, ref ์ ์ฅํ ๊ฐ์ ๋๋๋ง์ ์ผ์ผํค์ง ์์ => ref์ ๊ฐ์ด ๋ณํด๋ ๋ด๋ถ ๋ณ์๋ค์ด์ด๊ธฐํ ๋๋ ๊ฒ์ ๋ง์ ์ ์๋ค.์ฆ!!
state๋ ๋ฆฌ๋ ๋๋ง์ด ๊ผญ ํ์ํ ๊ฐ์ ๋ค๋ฃฐ ๋ ์ฐ๋ฉด ๋๋ค.
ref๋ ๋ฆฌ๋ ๋๋ง์ ๋ฐ์์ํค์ง ์๋ ๊ฐ์ ์ ์ฅํ ๋ ์ฌ์ฉํ๋ค.
useRef๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ ์ ํ์ง
state๋ ๋ณ๊ฒฝ๋๋ฉด ๋๋๋ง์ด ๋๊ณ ,ref๋ ๋ณ๊ฒฝ๋์ด๋ ๋ ๋๋ง์ด ์๋๋ค ๐จ
์๋ ์ฝ๋์์
plusStateCountButtonHandler ์ plusRefCountButtonHandler ๋ ๋ค๋ฅด๊ฒ ๋์ํ๋ค.
import "./App.css";
import { useRef, useState } from "react";
function App() {
const [count, setCount] = useState(0);
const countRef = useRef(0);
const plusStateCountButtonHandler = () => {
setCount(count + 1);
};
const plusRefCountButtonHandler = () => {
countRef.current++;
};
return (
<>
<div>
state ์์ญ์
๋๋ค. {count} <br />
<button onClick={plusStateCountButtonHandler}>state ์ฆ๊ฐ</button>
</div>
<div>
ref ์์ญ์
๋๋ค. {countRef.current} <br />
<button onClick={plusRefCountButtonHandler}>ref ์ฆ๊ฐ</button>
</div>
</>
);
}
export default App;
<input />ํ๊ทธ์๋ref๋ผ๋ ์์ฑ์ด ์๋ค. ์ด๊ฑธ ํตํด ํด๋น DOM ์์๋ก ์ ๊ทผํ ์ ์๋ค wow

import "./App.css";
function App() {
return (
<>
<div>
์์ด๋ : <input type="text" />
</div>
<div>
๋น๋ฐ๋ฒํธ : <input type="password" />
</div>
</>
);
}
export default App;
useRef ์ฌ์ฉํ์ฌ ํฌ์ปค์ฑ ์ฃผ๊ธฐ
import { useEffect, useRef } from "react";
import "./App.css";
function App() {
const idRef = useRef("");
// ๋ ๋๋ง์ด ๋ ๋
useEffect(() => {
idRef.current.focus();
}, []);
return (
<>
<div>
์์ด๋ : <input type="text" ref={idRef} />
</div>
<div>
๋น๋ฐ๋ฒํธ : <input type="password" />
</div>
</>
);
}
export default App;
์์ด๋๊ฐ 10์๋ฆฌ ์ ๋ ฅ๋๋ฉด ์๋์ผ๋ก ๋น๋ฐ๋ฒํธ ํ๋๋ก ์ด๋ํ๋ ์ฝ๋?!
import { useEffect, useRef, useState } from "react";
import "./App.css";
function App() {
const idRef = useRef("");
const pwRef = useRef("");
const [id, setId] = useState("");
const onIdChangeHandler = (event) => {
setId(event.target.value);
};
// ๋ ๋๋ง์ด ๋ ๋
useEffect(() => {
idRef.current.focus();
}, []);
// ์ useEffect ์์ ๋์์๊น์?
useEffect(() => {
if (id.length >= 10) {
pwRef.current.focus();
}
}, [id]);
return (
<>
<div>
์์ด๋ :
<input
type="text"
ref={idRef}
value={id}
onChange={onIdChangeHandler}
/>
</div>
<div>
๋น๋ฐ๋ฒํธ : <input type="password" ref={pwRef} />
</div>
</>
);
}
export default App;
id.length โฅ 10 ์ด ๋ก์ง์ useEffect ์์ ๋ฃ์๋์ง?
=> ๋ฐฐ์น ์
๋ฐ์ดํธ