ย
useRef
๋ ์์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.const refContainer = useRef(initialValue);
ย
์ปดํฌ๋ํธ์ โํ๊ทธ(tag)โ์ ์ง์ ์ ๊ทผํ๊ณ ์ถ์ ๋ useRef๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์๋ฐ์คํฌ๋ฆฝํธ์์ DOM element๋ฅผ ๊ฐ์ง๊ณ ์ฌ ๋
querySelector
๋ getElementById
๋ฅผ ์ฌ์ฉํ์ง๋ง React์์๋ useRef
๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๋ํ ๋ ๋๋ง๊ณผ ์๊ด์์ด ๊ฐ์ ๋ณ๊ฒฝํ๊ณ ์ถ์ ๋๋ useRef๋ฅผ ์ฌ์ฉํฉ๋๋ค.ย
์ด๋ useRef๋ฅผ ์ฌ์ฉํ์ฌ useRef๊ฐ ์ ๊ทผํ ํ๊ทธ ์์์ ๊ฐ์ ๋ฐ๊ฟ ๋๋ ๋ฆฌ๋ ๋๋ง์ ๋ฐ์์ํค์ง ์๋๋ค๋ ๊ฒ์ ๊ธฐ์ตํด์ผํฉ๋๋ค. ๋ฐ์ดํฐ, state์ ์๊ด์์ด(๋ฆฌ๋ ๋๋ง ํ์ง ์๊ณ ) component์ DOM์ ์ ์ดํ๊ณ ์ถ์ ๋ ref๋ฅผ ์ฌ์ฉํฉ๋๋ค. Ref๋ ๋จ์ฉํ์ง ์์ต๋๋ค. ๋จผ์ ref๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ์ ๊ตฌํํ ์ ์๋์ง ๊ณ ๋ คํ ํ ์ฌ์ฉํ์ธ์
ย
React์์๋
return
๋ฌธ์ ์๋ ์์๋ค์๊ฒ id๊ฐ์ ์ฃผ๊ณ ์ ํ์ ํจ์๋ฅผ ์ฌ์ฉํด๋ ์ข์ง๋ง(๊ธฐ์กด์ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฐฉ์), React์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ธ ์ฌ์ฌ์ฉ์ฑ์ ์๊ฐํ๋ค๋ฉด ์ฌ์ฉ์ ๊ถ์ฅํ์ง ์์ต๋๋ค. ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฌ์ฉํ ๊ฒฝ์ฐ ๋์ผํ id๊ฐ์ ๊ฐ์ง ์์๊ฐ ์๊ธฐ๊ธฐ ๋๋ฌธ์ด์ฃ ! (์ฌ์ฌ์ฉ ์ํ๋ค ํ๋๋ผ๋ ์ง์ id๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ค๋ณต์ ๊ฒฝ์ฐ๊ฐ ์๊ธธ ์ ์์ต๋๋ค.) ๊ทธ๋์ id๋ฅผ ์ฌ์ฉํ๋ ๋์ ref๋ฅผ ์ฌ์ฉํฉ๋๋ค. ref๋ ์ ์ญ์ผ๋ก ์๋ํ์ง์๊ณ ์ปดํฌ๋ํธ ๋ด๋ถ์์๋ง ์๋ํ๊ธฐ ๋๋ฌธ์
๋๋ค.(์ฌ์ฌ์ฉํ ๋ ๊ฐ์ ์ปดํฌ๋ํธ์ง๋ง ๋ค๋ฅด๊ฒ ์๋ํฉ๋๋ค.) ๋ง์ฝ id๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ๋๋ง๋ค id ๋ท๋ถ๋ถ์ id name์ ์ถ๊ฐํ์ฌ ์ ๋ํฌํ id๋ก ๋ง๋ค์ด์ค์ผ ํฉ๋๋ค.
ย
useRef ์ฌ์ฉํด ๋ด ์๋ค!
ย
- ์ด๋ฉ์ผ input๊ณผ ๋น๋ฐ๋ฒํธ input์ ์
๋ ฅ์ด ์๋ฃ๋ ํ ๋ก๊ทธ์ธ ๋ฒํผ์ ๋๋ฅด๋ฉด
span
๋ถ๋ถ์ ์ ๋ ฅํ๋ ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ณด์ฌ์ฃผ๋ ค๊ณ ํฉ๋๋ค.
ย
button์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด inputCheck์์ ๋น๋ฐ๋ฒํธ์ ์ด๋ฉ์ผ์ ๊ฐ์ ๋ฐ์์์ setEmailValue, setPwValue๋ฅผ ์คํํด์ค์ ์ํ๊ฐ์ ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋ฉ๋๋ค.
import React, { useState } from "react"; const App = () => { const [emailValue, setEmailValue] = useState(""); // email state ๊ฐ const [pwValue, setPwValue] = useState(""); // pw state ๊ฐ const inputCheck = (e) => { e.preventDefault(); }; return ( <form style={{ display: "flex", flexDirection: "column" }}> <label> ์ด๋ฉ์ผ : <input type="email" /> </label> <label> ๋น๋ฐ๋ฒํธ : <input type="password" /> </label> <button type="submit" style={{ width: "100px" }} onClick={inputCheck}> ๋ก๊ทธ์ธ </button> <span>์ ๋ ฅํ ์ด๋ฉ์ผ : {emailValue}</span> <span>์ ๋ ฅํ ๋น๋ฐ๋ฒํธ : {pwValue}</span> </form> ); }; export default App;
ย
์ด๋ input์ ๊ฐ์ ๊ฐ์ ธ์ฌ ๋ useRef๋ฅผ ์ฌ์ฉํฉ๋๋ค. useRef๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์์
import
React, { useState, useRef }
from
"react";
์ฝ๋๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.App ์ปดํฌ๋ํธ ์์ useRef๋ฅผ ์์ฑํด์ค๋๋ค.
const emailInput = useRef(null); // email input์ ๋ํ useRef const pwInput = useRef(null); // pw input์ ๋ํ useRef
ย
์์ฑํ useRef ๊ฐ์ ๊ฐ์ ๋ฐ์์ค๊ณ ์ถ์ ํ๊ทธ์๊ฒ
ref
props๋ก ์ ๋ฌํด์ค๋๋ค.<label> ์ด๋ฉ์ผ : <input type="email" ref={emailInput} /> </label> <label> ๋น๋ฐ๋ฒํธ : <input type="password" ref={pwInput} /> </label>
ย
๋ก๊ทธ์ธ๋ฒํผ์ ํด๋ฆญํ์ ๋ ์คํํ inputCheck์์ emailInput๊ณผ pwInput์ ์ถ๋ ฅํด๋ด
์๋ค.
์ด์ useRef.current.value๋ฅผ ํ๊ฒ๋๋ฉด inputํ๊ทธ๋ค์ ๊ฐ๋ค์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
email์ pw ๊ฐ์ useState๋ฅผ ์ฌ์ฉํด์ ์ํ ๊ฐ์ ๋ฐ๊ฟ์ค๋๋ค.
const inputCheck = (e) => { e.preventDefault(); console.log(emailInput); console.log(pwInput); setEmailValue(emailInput.current.value); setPwValue(pwInput.current.value); };
ย
import React, { useRef, useState } from "react"; const App = () => { const emailInput = useRef(null); // email input์ ๋ํ useRef const pwInput = useRef(null); // pw input์ ๋ํ useRef const [emailValue, setEmailValue] = useState(""); // email state ๊ฐ const [pwValue, setPwValue] = useState(""); // pw state ๊ฐ const inputCheck = (e) => { e.preventDefault(); setEmailValue(emailInput.current.value); setPwValue(pwInput.current.value); }; return ( <form style={{ display: "flex", flexDirection: "column" }}> <label> ์ด๋ฉ์ผ : <input type="email" ref={emailInput} /> </label> <label> ๋น๋ฐ๋ฒํธ : <input type="password" ref={pwInput} /> </label> <button type="submit" style={{ width: "100px" }} onClick={inputCheck}> ํ์๊ฐ์ </button> <span>{emailValue}</span> <span>{pwValue}</span> </form> ); }; export default App;
ย
ย
- ์ด๋ฉ์ผ input์ด๋ ๋น๋ฐ๋ฒํธ input์ ์ ๋ ฅ์ด ์๋ฃ๋์ง ์์ ์ฑ ๋ก๊ทธ์ธ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ ๋ ฅ๋์ง ์์ input ํ๊ทธ์ focusํด๋ณด๋ก ํฉ์๋ค.
ย
useRef๊ฐ ๊ฐ๋ฆฌํค๋ ํ๊ทธ์ ์ด๋ฒคํธ๋ฅผ ์ค ๋๋ current ๋ค์์ ์ด๋ฒคํธ๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋ฉ๋๋ค.
inputCheck
ํจ์ ์์์ ์์ฑํด์ฃผ์ธ์.if (emailInput.current.value === "") { alert("์ด๋ฉ์ผ์ ์ ๋ ฅํด์ฃผ์ธ์"); emailInput.current.focus(); return; // if, else ๋ฌธ์ ๋ค์ด์ค๊ฒ ๋๋ฉด setState ์คํ์์ด ๋ฐ๋ก return } else if (pwInput.current.value === "") { alert("๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์"); pwInput.current.focus(); return; }
ย
import React, { useRef, useState } from "react"; const App = () => { const emailInput = useRef(null); // email input์ ๋ํ useRef const pwInput = useRef(null); // pw input์ ๋ํ useRef const [emailValue, setEmailValue] = useState(""); // email state ๊ฐ const [pwValue, setPwValue] = useState(""); // pw state ๊ฐ const inputCheck = (e) => { e.preventDefault(); if (emailInput.current.value === "") { alert("์ด๋ฉ์ผ์ ์ ๋ ฅํด์ฃผ์ธ์"); emailInput.current.focus(); return; } else if (pwInput.current.value === "") { alert("๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์"); pwInput.current.focus(); return; } setEmailValue(emailInput.current.value); setPwValue(pwInput.current.value); }; return ( <form style={{ display: "flex", flexDirection: "column" }}> <label> ์ด๋ฉ์ผ : <input type="email" ref={emailInput} /> </label> <label> ๋น๋ฐ๋ฒํธ : <input type="password" ref={pwInput} /> </label> <button type="submit" style={{ width: "100px" }} onClick={inputCheck}> ํ์๊ฐ์ </button> <span>{emailValue}</span> <span>{pwValue}</span> </form> ); }; export default App;
ย