Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

Ref์˜ ํ™œ์šฉ๊ณผ useRef ๋ณธ๋ฌธ

React

Ref์˜ ํ™œ์šฉ๊ณผ useRef

kohi โ˜• 2023. 1. 2. 23:34

๐Ÿ’ก Ref


Ref๋Š” render ๋ฉ”์„œ๋“œ์—์„œ ์ƒ์„ฑ๋œ DOM ๋…ธ๋“œ๋‚˜ React ์—˜๋ฆฌ๋จผํŠธ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด HTML์„ ์ž‘์„ฑํ•  ๋•Œ, div ๋“ฑ์˜ DOM ์š”์†Œ์— ์ด๋ฆ„์„ ๋‹ฌ ๊ฒฝ์šฐ <div id = "my-id"> ์™€ ๊ฐ™์ด id๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด์ฒ˜๋Ÿผ React์—์„œ๋„ DOM์„ ์„ ํƒํ•ด ์ง์ ‘ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด Ref๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

Ref๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ

  • ํฌ์ปค์Šค, ํ…์ŠคํŠธ ์„ ํƒ์˜์—ญ, ํ˜น์€ ๋ฏธ๋””์–ด์˜ ์žฌ์ƒ์„ ๊ด€๋ฆฌํ•  ๋•Œ
  • ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ง์ ‘์ ์œผ๋กœ ์‹คํ–‰์‹œํ‚ฌ ๋•Œ
  • ์„œ๋“œ ํŒŒํ‹ฐ DOM ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ React์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๋•Œ

 

๐Ÿ’ก ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๋ฐฉ์‹ - Ref ํ™œ์šฉ


import React, { useRef } from "react";

export default function UncontrolledForm() {
    const inputRef = useRef();

    console.log(inputRef);

    function handleSubmit(e) {
        e.preventDefault();
        alert(inputRef.current.value);
    }

    return (
        <form onSubmit={handleSubmit}>
            <label>๋‹‰๋„ค์ž„: </label>
            <input type="text" name="nickname" ref={inputRef} />
            <input type="submit" value="์ œ์ถœ" />
        </form>
    );
}

 

 

๐Ÿ’ก Ref ์‹ฌํ™”


forwardRef๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ ref๋ฅผ ๋‹ค๋ฃจ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ DOM ๋…ธ๋“œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์€ ์ปดํฌ๋„ŒํŠธ์˜ ์บก์Šํ™”๋ฅผ ํŒŒ๊ดดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.

const FancyButton = React.forwardRef((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

// ์ด์ œ DOM ๋ฒ„ํŠผ์œผ๋กœ ref๋ฅผ ์ž‘์ ‘ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;

 

useRef๋Š” .current ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , useRef() hook์œผ๋กœ ๋งŒ๋“  ๋ณ€์ˆ˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ์•  ์ฃผ๊ธฐ๋ฅผ ํ†ตํ•ด ์œ ์ง€๋˜์ง€๋งŒ .current ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์–ด๋„ ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, DOM ๋…ธ๋“œ์— useRef()๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ref๋ฅผ ๋ถ™์ด๊ฑฐ๋‚˜ ๋–ผ์–ด๋„ ์ปดํฌ๋„ŒํŠธ๋Š” ์ธ์ง€ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

DOM ๋…ธ๋“œ์— ref๊ฐ€ attachํ•˜๊ฑฐ๋‚˜ detachํ•  ๋•Œ ์–ด๋–ค ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด callback ref ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ref์— useRef๋กœ ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ ๋„˜๊ธฐ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค.

 

import React, { useState, useCallback } from "react";
import Cat from "./components/Cat";
import "./styles.css";

export default function App() {
  const [height, setHeight] = useState(0);

  const callbackRef = useCallback((node) => {
    if (node !== null) {
      setHeight(node.getBoundingClientRect().height);
    }
  }, []);

  return (
    <div>
      <h4> ๊ณ ์–‘์ด๊ฐ€ ์„ธ์ƒ์„ ๊ตฌํ•œ๋‹ค ๏ธ</h4>
      <p> ๋‚ด ํ‚ค๋Š” : {height}px ์ด์•ผ</p>
      <div ref={callbackRef}>
        <Cat />
      </div>
    </div>
  );
}

 

๐Ÿ’ก Reference


https://ko.reactjs.org/docs/refs-and-the-dom.html#gatsby-focus-wrapper

 

Ref์™€ DOM – React

A JavaScript library for building user interfaces

ko.reactjs.org

https://leehwarang.github.io/2020/11/29/ref.html

 

React useRef์˜ ๋‹ค์–‘ํ•œ ํ™œ์šฉ ๋ฐฉ๋ฒ•(mutable object, callback ref์™€ forwardRef) - ์ดํ™”๋ž‘ ๋ธ”๋กœ๊ทธ

React useRef์˜ ๋‹ค์–‘ํ•œ ํ™œ์šฉ ๋ฐฉ๋ฒ•(mutable object, callback ref์™€ forwardRef) ๋ฆฌ์•กํŠธ์—์„œ render() ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง€๋Š” DOM์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹ ์œผ๋กœ ref ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์†ก์ง€ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„์•ผ ํ•˜

leehwarang.github.io