์ฃผ์˜ํ•˜์„ธ์š”!

createRef๋Š” ์ฃผ๋กœ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” useRef๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

createRef๋Š” ์ž„์˜์˜ ๊ฐ’์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ref ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

class MyInput extends Component {
inputRef = createRef();
// ...
}

์ฐธ์กฐ

createRef()

ref๋ฅผ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ์•ˆ์— ์„ ์–ธํ•˜๋ ค๋ฉด createRef๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

import { createRef, Component } from 'react';

class MyComponent extends Component {
intervalRef = createRef();
inputRef = createRef();
// ...

์•„๋ž˜์—์„œ ๋” ๋งŽ์€ ์˜ˆ์ œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜

createRef๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜๊ฐ’

createRef`๋Š” ๋‹จ์ผ ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • current: ์ฒ˜์Œ์—๋Š” null๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‚˜์ค‘์— ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ref ๊ฐ์ฒด๋ฅผ JSX ๋…ธ๋“œ์˜ ref ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋กœ React์— ์ „๋‹ฌํ•˜๋ฉด React๋Š” ์ด๋ฅผ current ํ”„๋กœํผํ‹ฐ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์˜

  • createRef๋Š” ํ•ญ์ƒ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” { current: null }์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ํ•ญ์ƒ ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” useRef๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • const ref = useRef()๋Š” const [ref, _] = useState(() => createRef(null))์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ref ์„ ์–ธํ•˜๊ธฐ

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋‚ด์—์„œ ์ฐธ์กฐ๋ฅผ ์„ ์–ธํ•˜๋ ค๋ฉด createRef๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ž˜์Šค ํ•„๋“œ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

import { Component, createRef } from 'react';

class Form extends Component {
inputRef = createRef();

// ...
}

์ด์ œ ref={this.inputRef}๋ฅผ JSX์— ์žˆ๋Š” <input>์— ์ „๋‹ฌํ•˜๋ฉด, React๋Š” this.inputRef.current๋ฅผ input DOM ๋…ธ๋“œ๊ฐ€ ์ฐจ์ง€ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด input์— ํฌ์ปค์‹ฑํ•˜๋Š” ๋ฒ„ํŠผ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import { Component, createRef } from 'react';

export default class Form extends Component {
  inputRef = createRef();

  handleClick = () => {
    this.inputRef.current.focus();
  }

  render() {
    return (
      <>
        <input ref={this.inputRef} />
        <button onClick={this.handleClick}>
          input์— ํฌ์ปค์Šค
        </button>
      </>
    );
  }
}

์ฃผ์˜ํ•˜์„ธ์š”!

createRef๋Š” ์ฃผ๋กœ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” useRef๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


๋Œ€์•ˆ

createRef๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์—์„œ useRef๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๊ธฐ

์ƒˆ๋กœ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. createRef๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์กด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ์ด๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์›๋ณธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

import { Component, createRef } from 'react';

export default class Form extends Component {
  inputRef = createRef();

  handleClick = () => {
    this.inputRef.current.focus();
  }

  render() {
    return (
      <>
        <input ref={this.inputRef} />
        <button onClick={this.handleClick}>
          input์— ํฌ์ปค์Šค
        </button>
      </>
    );
  }
}

import { useRef } from 'react';

export default function Form() {
  const inputRef = useRef(null);

  function handleClick() {
    inputRef.current.focus();
  }

  return (
    <>
      <input ref={inputRef} />
      <button onClick={handleClick}>
        input์— ํฌ์ปค์Šค
      </button>
    </>
  );
}