1. useRef : ๋ณ์ ๊ด๋ฆฌ
1.1 ๊ฐ๋
ํจ์ํ ์ปดํฌ๋ํธ์์ useRef๋ฅผ ๋ถ๋ฅด๋ฉด, ref ์ค๋ธ์ ํธ๋ฅผ ๋ฐํํฉ๋๋ค.
1const ref = useRef(value)23// ref ์ค๋ธ์ ํธ๋ { currnt: value } <- ์ด๋ฐ ์์ผ๋ก ์์ฑ๋จ4// useRef์ ์ธ์๋ก ๋ฃ์ด์ค value๋ ref์์ current์ ์ ์ฅ๋จ5// ref ์ค๋ธ์ ํธ๋ ์์ ์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์, ์ธ์ ๋ ์ํ๋ ๊ฐ์ผ๋ฃจ ์์ ํ ์ ์์
๋ฐํ๋ ref๋ ์ปดํฌ๋ํธ์ ์ ์ฒด ์์ ์ฃผ๊ธฐ๋ฅผ ํต๊ณผํด๋ ์ ์ง๊ฐ ๋ฉ๋๋ค.
์ฆ, ์ปดํฌ๋ํธ๊ฐ unmount(์ฐ๊ฒฐ ํด์ )๋๊ธฐ ์ ๊น์ง๋ ๊ฐ์ ๊ทธ๋๋ก ์ ์งํ ์ ์๋ค๋ ์๋ฆฌ์
๋๋ค.
useRef๊ฐ ์ ์ฉํ ์ํฉ 2๊ฐ์ง
- ref๊ฐ state์ ๋น์ทํ๊ฒ,
์ ์ฅ๊ณต๊ฐ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ- State์ ๋ณํ โ ๋ ๋๋ง โ ์ปดํฌ๋ํธ ๋ด๋ถ ๋ณ์๋ค ์ด๊ธฐํ
Ref์ ๋ณํ โ No ๋ ๋๋ง โ ๋ณ์๋ค์ ๊ฐ์ด ์ ์ง๋จ- State์ ๋ณํ โ ๋ ๋๋ง โ ๊ทธ๋๋ Ref์ ๊ฐ์ ์ ์ง๋จ
DOM ์์์ ์ ๊ทผํ๋ ๊ฒฝ์ฐ- e.g. Input์์๋ฅผ ํด๋ฆญํ์ง ์์๋, focus๋ฅผ ์ฃผ๊ณ ์ถ์ ๊ฒฝ์ฐ
- ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธํ๋ฉด์ ๋ค์ด๊ฐ์ ๋, ๋ง์ฐ์ค๋ก ์ ๋ ฅ ๋์ ํด๋ฆญํ์ง ์์๋,
- foucsํจ๊ณผ๋ฅผ ์๊ณ , ๋ฐ๋ก ํค๋ณด๋๋ก ์ ๋ ฅ์ ํ ์ ์๊ฒ ํด์ค
- ๋ง์น Vanila JS์
Document.querySelector()์ ์ญํ
- e.g. Input์์๋ฅผ ํด๋ฆญํ์ง ์์๋, focus๋ฅผ ์ฃผ๊ณ ์ถ์ ๊ฒฝ์ฐ
1.2 ์์ : Ref์ State ๋น๊ต
1import React, { useState, useRef } from 'react'23function App() {4// React์์ state๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฒ = ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง5const [count, setCount] = useState(0)67// Ref๋ ์๋ฌด๋ฆฌ ์์ ํด๋, ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ ๋๋งํ์ง ์์8const countRef = useRef(0) // countRef.current๋ก ์ ๊ทผ910console.log('๋ ๋๋ง')1112const increaseCountState = () => {13setCount(count + 1)14}1516const increaseCountRef = () => {17countRef.current = countRef.current + 118console.log('Ref :', countRef.current)19}2021return (22<div>23<p>State : {count}</p>24<p>Ref : {countRef.current}</p>25<button onClick={increaseCountState}>State ์ฌ๋ ค</button>26<button onClick={increaseCountRef}>Ref ์ฌ๋ ค</button>27</div>28)29}3031export default App
1.3 ์์ 2 : Ref์ ๋ณ์ ๋น๊ต
1import React, { useState, useRef } from 'react'23function App() {4// ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง ๋๋ค๋ ๊ฒ = ์ปดํฌ๋ํธ๋ฅผ ๋ํ๋ด๋ ํจ์๊ฐ ๋ค์ ๋ถ๋ฆฐ๋ค๋ ๊ฒ5// ํจ์๊ฐ ๋ถ๋ฆด ๋๋ง๋ค ํจ์ ๋ด๋ถ์ ๋ณ์๋ค์ ์ด๊ธฐํ๋จ6// ๊ทธ๋ ์ง๋ง Ref๋ ์๋ฌด๋ฆฌ ๋ ๋๋ง๋๋ ๊ฐ์ ์ ์งํจ7const [renderer, setRenderer] = useState(0)8const countRef = useRef(0)9let countVar = 01011const doRendering = () => {12setRenderer(renderer + 1)13}1415const increaseRef = () => {16countRef.current = countRef.current + 117console.log('ref: ', countRef.current)18}1920const increaseVar = () => {21countVar = countVar + 122console.log('var: ', countVar)23}2425const printResults = () => {26console.log(`ref : ${countRef.current}, var: ${countVar}`)27}2829return (30<div>31<p>Ref : {countRef.current}</p>32<p>Var : {countVar}</p>33<button onClick={doRendering}>๋ ๋!</button>34<button onClick={increaseRef}>Ref ์ฌ๋ ค</button>35<button onClick={increaseVar}>Var ์ฌ๋ ค</button>36<button onClick={printResults}>Ref, Var ๊ฐ ์ถ๋ ฅ</button>37</div>38)39}4041export default App
1.4 ์์ 3 : ๋ฌดํ๋ฃจํ
1import React, { useState, useRef, useEffect } from 'react'23function App() {4const [count, setCount] = useState(1)5const renderCount = useRef(1)67// ๋ฌดํ ๋ฃจํ์ ๋น ์ง, State๋ผ๋ฆฌ ์๋ก ๋ ๋๋ง๋์ด์8// const [renderCount, setRenderCount] = useState(1);9// useEffect(() => {10// console.log('๋ ๋๋ง');11// setRenderCount(renderCount + 1);12// });1314// Ref๋ ๋ฆฌ๋ ๋๋ง์ ๋ฐ์์ํค์ง ์์ผ๋๊น, ๋ฌดํ๋ฃจํ์ ์๋น ์ง15useEffect(() => {16renderCount.current = renderCount.current + 117console.log('๋ ๋๋ง ์ :', renderCount.current)18})1920return (21<div>22<p>countVar : {count}</p>23<button onClick={() => setCount(count + 1)}>์ฌ๋ ค!</button>24</div>25)26}2728export default App
2. useRef : DOM ์์ ์ ๊ทผ
1import React, { useEffect, useRef } from 'react'23function App() {4const inputRef = useRef()56useEffect(() => {7// console.log(inputRef);8inputRef.current.focus() // ๋ ๋๋ง๋๋ฉด ์๋์ผ๋ก focus๋จ9}, [])1011const login = () => {12alert(`ํ์ํฉ๋๋ค ${inputRef.current.value}`)13inputRef.current.focus()14}1516return (17<div>18<input ref={inputRef} type="text" placeholder="username" />19<button onClick={login}>๋ก๊ทธ์ธ</button>20</div>21)22}2324export default App