๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[React] useRef (2023.05.24) ๋ณธ๋ฌธ

full stack/React

[React] useRef (2023.05.24)

Shinjio 2023. 5. 24. 16:13

 

useRef

 

๐ŸŽˆ useRef ์˜ˆ์ œ

 ์ž‘๊ฒŒ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘๊ฒŒ, ์›๋ณธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์›๋ณธ ํฌ๊ธฐ๋กœ, ํฌ๊ฒŒ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๊ฐ€ ํฌ๊ฒŒ ๋ณ€ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

 

 

๐Ÿ“– ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ์œ„ํ•ด ์‚ฌ๊ฐํ˜• ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ document์— ์ ‘๊ทผํ•ด์•ผ ํ–ˆ์Œ

 

์‚ฌ๊ฐํ˜• ์š”์†Œ :

<div id = "box"></div>

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ id๊ฐ€ box์ธ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ :

document.getElementById('box").style.width= 100px

 

๐Ÿ“– ๋ฆฌ์•กํŠธ์—์„œ ๋ณ€๊ฒฝ์‹œํ‚ค๋ ค๋ฉด?

DOM์œผ๋กœ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ‘๊ทผํ•ด์•ผํ•จ

useRef ์‚ฌ์šฉ

 

usdRef : ํŠน์ • DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ   
- ๋ฐ”๋‹๋ผJS์—์„œ๋Š” getElementById, querySelector ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜์˜€์Œ   
- React๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํŠน์ • DOM ์š”์†Œ์— ์ง์ ‘ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ์ผ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ       
    ex. ํฌ์ปค์‹ฑ ์ž‘์—…, ์†์„ฑ ๋ณ€๊ฒฝ ๋“ฑ     
    >> useRef๋ผ๋Š” react Hook์„ ์‚ฌ์šฉ

 

โš™๏ธ useRef ์‚ฌ์šฉํ•˜๊ธฐ

์ƒ์œ„์— useRef ์ •์˜

const useRefName = useRef();

 

DOM์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ(ํƒœ๊ทธ)์— ref = {useRef๋ช…} ์†์„ฑ์œผ๋กœ ์ถ”๊ฐ€

<div id="box" ref={useRefName}>

 

DOM์— ์ ‘๊ทผํ•˜์—ฌ ์†์„ฑ ๋ฐ”๊พธ๊ธฐ

=> useRef๋ช….current...

ex. input ํƒœ๊ทธ์˜ ์ž…๋ ฅ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ• : useRefName.current.value

      img ํƒœ๊ทธ์˜ ๊ฒฝ๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ• : useRefName.current.src

useRefName.current.style.width = "100px"

 

import React, { useRef } from "react";
import Ex05ButtonList from "./components/Ex05ButtonList";

const Ex05 = () => {
  /* usdRef : ํŠน์ • DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
    - ๋ฐ”๋‹๋ผJS์—์„œ๋Š” getElementById, querySelector ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜์˜€์Œ
    - React๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํŠน์ • DOM ์š”์†Œ์— ์ง์ ‘ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ์ผ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ
        ex. ํฌ์ปค์‹ฑ ์ž‘์—…, ์†์„ฑ ๋ณ€๊ฒฝ ๋“ฑ
      >> useRef๋ผ๋Š” react Hook์„ ์‚ฌ์šฉํ•  ๊ฒƒ
    */

  //divRef๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ DOM ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜จ ๊ฒƒ
  const divRef = useRef();

  const changeSize = (size) => {
    /** div์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์ฝ”๋“œ */
    divRef.current.style.width = `${size}px`;
    divRef.current.style.height = `${size}px`;
  };

  const chBox = (e) => {
    console.log("change Box Fuction", e.target.innerText);
    // console.log("divRef : ", divRef.current.style.width);

    if (e.target.innerText == "์ž‘๊ฒŒ") {
      changeSize(100);
    } else if (e.target.innerText == "์›๋ณธ") {
      changeSize(159);
    } else {
      changeSize(200);
    }
  };

  return (
    <div>
      <Ex05ButtonList chBox={chBox} />
      <div
        ref={divRef}
        style={{ backgroundColor: "skyblue", width: "150px", height: "150px" }}
      ></div>
    </div>
  );
};

export default Ex05;

 

 

๐ŸŽˆ useRef ์‹ค์Šต 1

๋ณ€๊ฒฝ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ํ•™์Šต์ž๋ฃŒ pdf ํŒŒ์ผ 1์—์„œ 2๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

useRef๋ฅผ ์ด์šฉํ•ด์„œ pdf1 ๋ทฐ์–ด๋ฅผ pdf2๊ฐ€ ๋ณด์ด๋„๋ก ํ•  ๊ฒƒ

๋‹จ, state ์‚ฌ์šฉ ๊ธˆ์ง€

 

 

'/pdf/pdf1.pdf'

>> "http://localhost:3000/pdf/pdf1.pdf"

 

import React, { useRef } from "react";

const Ex06 = () => {
  const iframeRef = useRef();

  let pdfStyle = {
    // ๋‚˜๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ถ€๋ชจ๋ฅผ ๊ธฐ์ค€์œผ๋กœ 100% (div - ๊ฐ€๋กœํ•œ์ค„)
    width: "100%",
    height: "90%",
  };

  const chPdf = () => {
    console.log("chPdf", iframeRef.current.src);
    iframeRef.current.src == "http://localhost:3000/pdf/pdf1.pdf"
      ? (iframeRef.current.src = "http://localhost:3000/pdf/pdf2.pdf")
      : (iframeRef.current.src = "http://localhost:3000/pdf/pdf1.pdf");
  };

  return (
    // vh : ๋‚ด ๋ˆˆ์— ๋ณด์ด๋Š” ๊ธฐ์ค€
    <div style={{ height: "100vh" }}>
      <h3>
        ์˜ค๋Š˜์˜ ํ•™์Šต์ž๋ฃŒ
        <button onClick={chPdf}>๋ณ€๊ฒฝ</button>
      </h3>
      <iframe ref={iframeRef} style={pdfStyle} src="/pdf/pdf1.pdf"></iframe>
    </div>
  );
};

export default Ex06;

 

๐ŸŽˆ useRef ์‹ค์Šต 2

ํ•œ์†Œํฌ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋ณ€๊ฒฝ ํด๋ฆญํ•˜๋ฉด ์‚ฌ์ง„ ๋ณ€๊ฒฝ ํ›„ input ํƒœ๊ทธ์— ํฌ์ปค์‹ฑ

 

 

import React, { useRef } from "react";

const Ex07 = () => {
  /**
     1) ์‚ฌ์šฉ์ž๊ฐ€ input ํƒœ๊ทธ ์•ˆ์— ์ž…๋ ฅํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ
     2) ๊ทธ ๊ฐ’์— ๋”ฐ๋ผ์„œ ์‚ฌ์ง„์˜ src ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๊ธฐ
     */

  const inputRef = useRef();
  const imgRef = useRef();

  const chPic = () => {
    if (inputRef.current.value == "ํ•œ์†Œํฌ") {
      imgRef.current.src =
        "https://file.mk.co.kr/meet/neds/2021/10/image_readtop_2021_1006877_16351128614821782.jpg";
    } else {
      imgRef.current.src =
        "https://cdn.hankooki.com/news/photo/202301/47163_63325_1673949170.jpg";
    }
    inputRef.current.value = "";
    inputRef.current.focus();
  };

  return (
    <div>
      <p>ํฌ๋งํ•˜๋Š” ์‚ฌ์ง„์ด ์žˆ์œผ์‹ ๊ฐ€์š”?</p>
      <input ref={inputRef} type="text" />
      <button onClick={chPic}>๋ณ€๊ฒฝ!</button>

      <div>
        <img
          ref={imgRef}
          src="https://cdn.hankooki.com/news/photo/202301/47163_63325_1673949170.jpg"
          width="300px"
        ></img>
      </div>
    </div>
  );
};

export default Ex07;