๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Front
10-useRef

1. useRef : ๋ณ€์ˆ˜ ๊ด€๋ฆฌ

1.1 ๊ฐœ๋…

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ useRef๋ฅผ ๋ถ€๋ฅด๋ฉด, ref ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

1
const ref = useRef(value)
2
3
// ref ์˜ค๋ธŒ์ ํŠธ๋Š” { currnt: value } <- ์ด๋Ÿฐ ์‹์œผ๋กœ ์ƒ์„ฑ๋จ
4
// useRef์˜ ์ธ์ž๋กœ ๋„ฃ์–ด์ค€ value๋Š” ref์•ˆ์— current์— ์ €์žฅ๋จ
5
// ref ์˜ค๋ธŒ์ ํŠธ๋Š” ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์–ธ์ œ๋“  ์›ํ•˜๋Š” ๊ฐ’์œผ๋ฃจ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Œ

๋ฐ˜ํ™˜๋œ ref๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ „์ฒด ์ƒ์• ์ฃผ๊ธฐ๋ฅผ ํ†ต๊ณผํ•ด๋„ ์œ ์ง€๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ปดํฌ๋„ŒํŠธ๊ฐ€ unmount(์—ฐ๊ฒฐ ํ•ด์ œ)๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์†Œ๋ฆฌ์ž…๋‹ˆ๋‹ค.

useRef๊ฐ€ ์œ ์šฉํ•œ ์ƒํ™ฉ 2๊ฐ€์ง€

  1. ref๊ฐ€ state์™€ ๋น„์Šทํ•˜๊ฒŒ, ์ €์žฅ๊ณต๊ฐ„์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
    • State์˜ ๋ณ€ํ™” โ†’ ๋ Œ๋”๋ง โ†’ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ ๋ณ€์ˆ˜๋“ค ์ดˆ๊ธฐํ™”
    • Ref์˜ ๋ณ€ํ™” โ†’ No ๋ Œ๋”๋ง โ†’ ๋ณ€์ˆ˜๋“ค์˜ ๊ฐ’์ด ์œ ์ง€๋จ
    • State์˜ ๋ณ€ํ™” โ†’ ๋ Œ๋”๋ง โ†’ ๊ทธ๋ž˜๋„ Ref์˜ ๊ฐ’์€ ์œ ์ง€๋จ
  2. DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ
    • e.g. Input์š”์†Œ๋ฅผ ํด๋ฆญํ•˜์ง€ ์•Š์•„๋„, focus๋ฅผ ์ฃผ๊ณ ์‹ถ์€ ๊ฒฝ์šฐ
      • ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธํ™”๋ฉด์— ๋“ค์–ด๊ฐ”์„ ๋–„, ๋งˆ์šฐ์Šค๋กœ ์ž…๋ ฅ ๋ž€์„ ํด๋ฆญํ•˜์ง€ ์•Š์•„๋„,
      • foucsํšจ๊ณผ๋ฅผ ์žˆ๊ณ , ๋ฐ”๋กœ ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ
      • ๋งˆ์น˜ Vanila JS์˜ Document.querySelector()์˜ ์—ญํ• 

1.2 ์˜ˆ์ œ : Ref์™€ State ๋น„๊ต

1
import React, { useState, useRef } from 'react'
2
3
function App() {
4
// React์—์„œ state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ = ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง
5
const [count, setCount] = useState(0)
6
7
// Ref๋Š” ์•„๋ฌด๋ฆฌ ์ˆ˜์ •ํ•ด๋„, ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ๋ Œ๋”๋งํ•˜์ง€ ์•Š์Œ
8
const countRef = useRef(0) // countRef.current๋กœ ์ ‘๊ทผ
9
10
console.log('๋ Œ๋”๋ง')
11
12
const increaseCountState = () => {
13
setCount(count + 1)
14
}
15
16
const increaseCountRef = () => {
17
countRef.current = countRef.current + 1
18
console.log('Ref :', countRef.current)
19
}
20
21
return (
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
}
30
31
export default App

1.3 ์˜ˆ์ œ2 : Ref์™€ ๋ณ€์ˆ˜ ๋น„๊ต

1
import React, { useState, useRef } from 'react'
2
3
function App() {
4
// ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋œ๋‹ค๋Š” ๊ฒƒ = ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‹ค์‹œ ๋ถˆ๋ฆฐ๋‹ค๋Š” ๊ฒƒ
5
// ํ•จ์ˆ˜๊ฐ€ ๋ถˆ๋ฆด ๋–„๋งˆ๋‹ค ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๋ณ€์ˆ˜๋“ค์€ ์ดˆ๊ธฐํ™”๋จ
6
// ๊ทธ๋ ‡์ง€๋งŒ Ref๋Š” ์•„๋ฌด๋ฆฌ ๋ Œ๋”๋ง๋˜๋„ ๊ฐ’์„ ์œ ์ง€ํ•จ
7
const [renderer, setRenderer] = useState(0)
8
const countRef = useRef(0)
9
let countVar = 0
10
11
const doRendering = () => {
12
setRenderer(renderer + 1)
13
}
14
15
const increaseRef = () => {
16
countRef.current = countRef.current + 1
17
console.log('ref: ', countRef.current)
18
}
19
20
const increaseVar = () => {
21
countVar = countVar + 1
22
console.log('var: ', countVar)
23
}
24
25
const printResults = () => {
26
console.log(`ref : ${countRef.current}, var: ${countVar}`)
27
}
28
29
return (
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
}
40
41
export default App

1.4 ์˜ˆ์ œ3 : ๋ฌดํ•œ๋ฃจํ”„

1
import React, { useState, useRef, useEffect } from 'react'
2
3
function App() {
4
const [count, setCount] = useState(1)
5
const renderCount = useRef(1)
6
7
// ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ง, State๋ผ๋ฆฌ ์„œ๋กœ ๋ Œ๋”๋ง๋˜์–ด์„œ
8
// const [renderCount, setRenderCount] = useState(1);
9
// useEffect(() => {
10
// console.log('๋ Œ๋”๋ง');
11
// setRenderCount(renderCount + 1);
12
// });
13
14
// Ref๋Š” ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์œผ๋‹ˆ๊นŒ, ๋ฌดํ•œ๋ฃจํ”„์— ์•ˆ๋น ์ง
15
useEffect(() => {
16
renderCount.current = renderCount.current + 1
17
console.log('๋ Œ๋”๋ง ์ˆ˜ :', renderCount.current)
18
})
19
20
return (
21
<div>
22
<p>countVar : {count}</p>
23
<button onClick={() => setCount(count + 1)}>์˜ฌ๋ ค!</button>
24
</div>
25
)
26
}
27
28
export default App

2. useRef : DOM ์š”์†Œ ์ ‘๊ทผ

1
import React, { useEffect, useRef } from 'react'
2
3
function App() {
4
const inputRef = useRef()
5
6
useEffect(() => {
7
// console.log(inputRef);
8
inputRef.current.focus() // ๋ Œ๋”๋ง๋˜๋ฉด ์ž๋™์œผ๋กœ focus๋จ
9
}, [])
10
11
const login = () => {
12
alert(`ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค ${inputRef.current.value}`)
13
inputRef.current.focus()
14
}
15
16
return (
17
<div>
18
<input ref={inputRef} type="text" placeholder="username" />
19
<button onClick={login}>๋กœ๊ทธ์ธ</button>
20
</div>
21
)
22
}
23
24
export default App

[์ฐธ๊ณ ]