๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

์ด API๋Š” ํ–ฅํ›„ React์˜ ์ฃผ์š” ๋ฒ„์ „์—์„œ ์ œ๊ฑฐ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋Œ€์•ˆ๋“ค์„ ์‚ดํŽด๋ณด์„ธ์š”.

findDOMNode๋Š” React์˜ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ธŒ๋ผ์šฐ์ €์˜ DOM ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

const domNode = findDOMNode(componentInstance)

๋ ˆํผ๋Ÿฐ์Šค

findDOMNode(componentInstance)

findDOMNode๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ฃผ์–ด์ง„ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ธŒ๋ผ์šฐ์ €์˜ DOM ๋…ธ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

import { findDOMNode } from 'react-dom';

const domNode = findDOMNode(componentInstance);

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

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

  • componentInstance: ์ปดํฌ๋„ŒํŠธ ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ this๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜

findDOMNode๋Š” ์ฃผ์–ด์ง„ componentInstance์—์„œ ๊ฐ€์žฅ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ € DOM ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ null์ด๋‚˜ false๋ฅผ ๋ Œ๋”๋งํ•  ๊ฒฝ์šฐ findDOMNode๋Š” null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฌธ์ž์—ด์„ ๋ Œ๋”๋งํ•˜๋ฉด findDOMNode๋Š” ๊ทธ ๊ฐ’์„ ํฌํ•จํ•œ ํ…์ŠคํŠธ DOM ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐฐ์—ด์ด๋‚˜ ๋‹ค์ˆ˜์˜ ์ž์‹์„ ๊ฐ€์ง„ Fragment๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ findDOMNode๋Š” ํ•ด๋‹นํ•˜๋Š” DOM ๋…ธ๋“œ ์ค‘ ๋น„์–ด์žˆ์ง€ ์•Š์€ ์ฒซ ๋ฒˆ์งธ ์ž์‹์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • findDOMNode๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ๋˜์—ˆ์„ ๋•Œ(์ฆ‰, ์ปดํฌ๋„ŒํŠธ๊ฐ€ DOM์— ๋ฐฐ์น˜๋˜์—ˆ์„ ๋•Œ)๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„์ง ๋งˆ์šดํŠธ ๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ (์˜ˆ์‹œ๋กœ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ์ „์ธ render() ๋‚ด์—์„œ findDOMNode()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ) ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

  • findDOMNode๋Š” ํ˜ธ์ถœํ•  ๋‹น์‹œ์˜ ๊ฒฐ๊ณผ๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„์— ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ๋ Œ๋”๋งํ•˜๋”๋ผ๋„, ๊ทธ ๋ณ€ํ™”์— ๋Œ€ํ•ด ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  • findDOMNode๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.


์‚ฌ์šฉ๋ฒ•

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ root DOM ๋…ธ๋“œ๋ฅผ ์ฐพ๊ธฐ

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ์ธ์Šคํ„ด์Šค์—์„œ findDOMNode๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ (์ฃผ๋กœ this) ์ด๋ฅผ ๋ Œ๋”๋งํ•œ DOM ๋…ธ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

class AutoselectingInput extends Component {
componentDidMount() {
const input = findDOMNode(this);
input.select()
}

render() {
return <input defaultValue="Hello" />
}
}

์—ฌ๊ธฐ์„œ input ๋ณ€์ˆ˜๋Š” <input> DOM ์š”์†Œ๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด๊ฒƒ์„ ํ†ตํ•ด ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€œShow exampleโ€์„ ํด๋ฆญํ•˜์—ฌ ์ž…๋ ฅ์ฐฝ์„ ๋งˆ์šดํŠธํ•˜๊ณ , input.select()๋กœ ์ž…๋ ฅ์ฐฝ์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์„ ํƒํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

import { Component } from 'react';
import { findDOMNode } from 'react-dom';

class AutoselectingInput extends Component {
  componentDidMount() {
    const input = findDOMNode(this);
    input.select()
  }

  render() {
    return <input defaultValue="Hello" />
  }
}

export default AutoselectingInput;


๋Œ€์•ˆ

ref๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ DOM ๋…ธ๋“œ ์ฝ๊ธฐ

findDOMNode๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š” JSX ๋…ธ๋“œ์™€ ์ด์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ์ด ๋ช…์‹œ์ ์ด์ง€ ์•Š์•„ ์ทจ์•ฝํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋กœ <input />๋ฅผ <div>๋กœ ๊ฐ์‹ธ๋ณด์„ธ์š”.

import { Component } from 'react';
import { findDOMNode } from 'react-dom';

class AutoselectingInput extends Component {
  componentDidMount() {
    const input = findDOMNode(this);
    input.select()
  }
  render() {
    return <input defaultValue="Hello" />
  }
}

export default AutoselectingInput;

์ด์ œ ์ฝ”๋“œ๊ฐ€ ๊นจ์งˆ ํ…๋ฐ, ์ฝ”๋“œ๋Š” findDOMNode(this)๊ฐ€ <input> DOM ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ์—ˆ์ง€๋งŒ ์‹ค์ œ๋กœ ์ฐพ์€ ๊ฒƒ์€ <div> DOM ๋…ธ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํŠน์ • DOM ๋…ธ๋“œ๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ๋Š” createRef๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

์ด ์˜ˆ์‹œ์—์„  findDOMNode๋Š” ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  inputRef = createRef(null)๋ฅผ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ํ•„๋“œ์— ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ this.inputRef.current๋ฅผ ํ†ตํ•ด DOM ๋…ธ๋“œ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ JSX์— ๋ถ™์ด๋ ค๋ฉด, <input ref={this.inputRef} />๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ DOM ๋…ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์™€ JSX๋ฅผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

import { createRef, Component } from 'react';

class AutoselectingInput extends Component {
  inputRef = createRef(null);

  componentDidMount() {
    const input = this.inputRef.current;
    input.select()
  }

  render() {
    return (
      <input ref={this.inputRef} defaultValue="Hello" />
    );
  }
}

export default AutoselectingInput;

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ตœ์‹  React์—์„œ๋Š”, useRef๋ฅผ ๋Œ€์‹  ํ˜ธ์ถœํ•˜์—ฌ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

import { useRef, useEffect } from 'react';

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

  useEffect(() => {
    const input = inputRef.current;
    input.select();
  }, []);

  return <input ref={inputRef} defaultValue="Hello" />
}

refs๋ฅผ ์‚ฌ์šฉํ•œ DOM ์กฐ์ž‘์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด์„ธ์š”.


forwarded ref๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ DOM ๋…ธ๋“œ๋ฅผ ์ฝ๊ธฐ

์ด ์˜ˆ์‹œ์—์„œ๋Š” findDOMNode(this)๊ฐ€ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์— ์†ํ•œ DOM ๋…ธ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. AutoselectingInput ์ปดํฌ๋„ŒํŠธ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ <input>์„ ๋ Œ๋”๋งํ•˜๋Š” MyInput ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.

import { Component } from 'react';
import { findDOMNode } from 'react-dom';
import MyInput from './MyInput.js';

class AutoselectingInput extends Component {
  componentDidMount() {
    const input = findDOMNode(this);
    input.select()
  }
  render() {
    return <MyInput />;
  }
}

export default AutoselectingInput;

AutoselectingInput ์ปดํฌ๋„ŒํŠธ์˜ ๋‚ด๋ถ€์—์„œ findDOMNode(this)๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, <input>์— ๋Œ€ํ•œ JSX๊ฐ€ MyInput ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— ์ˆจ๊ฒจ์ ธ ์žˆ๋”๋ผ๋„ ๋ฌธ์ œ์—†์ด <input> DOM ๋…ธ๋“œ๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋™์ž‘์ด ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” ํŽธ๋ฆฌํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๊ฒฐ๊ตญ์€ ์ทจ์•ฝํ•œ ์ฝ”๋“œ๋กœ ๊ฐ€๋Š” ๋ฐ‘๊ฑฐ๋ฆ„์ด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‚˜์ค‘์— ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉฐ MyInput์„ <div>๋กœ ๊ฐ์‹ผ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”. <input>์„ ์ฐพ์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•˜๋˜ AutoselectingInput์˜ ์ฝ”๋“œ๋ฅผ ๊นจ๋œจ๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์‹œ์˜ findDOMNode๋ฅผ ๋Œ€์ฒดํ•˜๋ ค๋ฉด, ๋‘ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ˜‘๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. AutoSelectingInput์—์„œ๋Š” ref๋ฅผ ์„ ์–ธํ•ด์•ผ ํ•˜๊ณ , ์ด์ „์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ <MyInput>์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  2. MyInput์€ forwardRef๋กœ ์„ ์–ธํ•ด์•ผ ํ•˜๋ฉฐ, ํ•ด๋‹น ref๋ฅผ ๊ฐ€์ ธ์™€ <input> ๋…ธ๋“œ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ด์ƒ findDOMNode๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

import { createRef, Component } from 'react';
import MyInput from './MyInput.js';

class AutoselectingInput extends Component {
  inputRef = createRef(null);

  componentDidMount() {
    const input = this.inputRef.current;
    input.select()
  }

  render() {
    return (
      <MyInput ref={this.inputRef} />
    );
  }
}

export default AutoselectingInput;

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ด๋Ÿฐ ์ฝ”๋“œ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

import { useRef, useEffect } from 'react';
import MyInput from './MyInput.js';

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

  useEffect(() => {
    const input = inputRef.current;
    input.select();
  }, []);

  return <MyInput ref={inputRef} defaultValue="Hello" />
}


๊ฐ์‹ธ์ฃผ๋Š” <div> ์š”์†Œ ์ถ”๊ฐ€ํ•˜๊ธฐ

์ปดํฌ๋„ŒํŠธ๋Š” ์ข…์ข… ์ž์‹์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์•Œ์•„์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— findDOMNode(this)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹๋“ค์„ ์ฐพ๊ณ , ์ธก์ •์„ ์œ„ํ•ด getBoundingClientRect์™€ ๊ฐ™์€ DOM ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์ง์ ‘์ ์œผ๋กœ ๋Œ€์‘๋˜๋Š” ๋ฐฉ๋ฒ•์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— findDOMNode๋Š” ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Œ์—๋„ React์—์„œ ์™„์ „ํžˆ ์‚ฌ๋ผ์ง€์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋™์•ˆ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ฝ˜ํ…์ธ  ์ฃผ์œ„๋ฅผ <div> ๋…ธ๋“œ๋กœ ๊ฐ์‹ธ์ฃผ๊ณ , ๊ทธ ๋…ธ๋“œ์˜ ref๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ์‹ธ๋Š” ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์Šคํƒ€์ผ์„ ์†์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<div ref={someRef}>
{children}
</div>

์ด๋Š” ํŠน์ • ํ•ญ๋ชฉ์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ์Šคํฌ๋กค์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.