๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
WEB/REACT

[React] ref

by mingzoo 2021. 1. 1.

์˜ค๋Š˜์€ ref์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด๋ณด์•˜๋‹ค. 

๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ  ์ฑ…์„ ๋ณด๋ฉด์„œ ๊ณต๋ถ€๋ฅผ ํ•˜๋‹ค๊ฐ€ ref์— ๋Œ€ํ•ด์„œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

ko.reactjs.org/docs/refs-and-the-dom.html

 

ref(Reference)

ref๋Š” ๋ ˆํผ๋Ÿฐ์Šค๋ผ๋Š” ๋œป์ธ๋ฐ, ์ด๋ฅผ ๋ฒˆ์—ญํ•˜๋ฉด ์ฐธ์กฐ๋ผ๋Š” ๋ง์ด๋‹ค.

๊ณผ์—ฐ ์ฝ”๋”ฉ์—์„œ ์ฐธ์กฐ๋ผ๋Š” ๊ฒƒ์€ ์–ด๋–ค ๊ฒƒ์„ ๋œปํ•˜๋Š” ๊ฒƒ์ผ๊นŒ?

์›๋ž˜ ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ๋ฐฐ์šธ ๋•Œ, ์ฐธ์กฐ๋Š” ๋ณต์ œ์™€ ๋น„๊ต๊ฐ€ ๋˜์—ˆ์—ˆ๋‹ค.

๋ณต์ œ๋Š” ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด, ์ฐธ์กฐ๋Š” ๋ฐ”๋กœ๊ฐ€๊ธฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค๊ณ  ๋น„์œ ๋˜์—ˆ๋‹ค.

์ฐธ์กฐ๋Š” ์›๋ณธ ํŒŒ์ผ์— ๋Œ€ํ•œ ์ฃผ์†Œ ๊ฐ’์ด ๋‹ด๊ฒจ์žˆ์–ด์„œ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ด ๋ฐ”๋กœ๊ฐ€๊ธฐ์— ์ ‘๊ทผํ–ˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์— ์ €์žฅ๋œ ์›๋ณธ์˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•ด์„œ ์›๋ณธ์˜ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด๊ณ  ์›๋ณธ์— ๋Œ€ํ•œ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, ์›๊ฒฉ์œผ๋กœ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค๐Ÿค”

 

ref๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ !

DOM์„ ๊ผญ ์ง์ ‘์ ์œผ๋กœ ๊ฑด๋“œ๋ ค์•ผ ํ•  ๋•Œ

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

state๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ์ง€๋งŒ, state๋งŒ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์— ์ ์šฉํ•œ๋‹ค.

 

์—ฌ๊ธฐ์„œ input์— ํฌ์ปค์Šค๋ฅผ ์ฃผ๋Š” ์˜ˆ์ œ๋ฅผ ํ•ด๋ณผ๊ฑด๋ฐ, ๋‚˜๋Š” input์— ํฌ์ปค์Šค๋ฅผ ์ค€๋‹ค๋Š” ๋ง์˜ ์˜๋ฏธ๋ฅผ ์ฒ˜์Œ์— ๋ชฐ๋ž๋‹ค.

input์— ํฌ์ปค์Šค๋ฅผ ์ค€๋‹ค๋Š” ๋ง์€!  

๋ณดํ†ต input์ฐฝ์„ ๊ตฌํ˜„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์–ด๋„ฃ๊ณ  ์˜†์— ์ž…๋ ฅ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฒŒ ๋˜๋ฉด ์ปค์„œ๊ฐ€ ๊นœ๋นก์ด๋Š” ํšจ๊ณผ๊ฐ€ ์‚ฌ๋ผ์ ธ ๋‹ค์‹œ input๊ฐ’์„ ๋„ฃ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ๋‹ค์‹œ input์ฐฝ์„ ๋ˆ„๋ฅด๊ณ  ์ž‘์„ฑํ•ด์•ผ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, input์ฐฝ์— ํฌ์ปค์Šค๋ฅผ ์ฃผ๊ฒŒ ๋˜๋ฉด ์˜ˆ์ œ์—์„œ์˜ '๊ฒ€์ •ํ•˜๊ธฐ'๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ ๋„ ๋ฐ”๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฒ€์ •ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค!!

 

//ref ์‚ฌ์šฉ ์ „ ์ฝ”๋“œ

import React, { Component } from 'react';
import './ValidationSample.css';

class ValidationSample extends Component {
    state = {
        password: '',
        clicked: false,
        validated: false
    }

    handleChange = (e) => {
        this.setState({
            password: e.target.value
        });
    }

    handleButtonClick = () => {
        this.setState({
            clicked: true,
            validated: this.state.password === '0000',  //password๊ฐ€ 0000์ผ๋•Œ true๋กœ ๋ฐ˜ํ™˜
            password: ''
        })
    }

    onKeyPress = (e) => {
        if (e.key === 'Enter') {
            this.handleButtonClick();
        }
    }

    render() {
        return (
            <div>
                <input
                    type='password'
                    value={this.state.password}
                    onChange={this.handleChange}
                    className={this.state.clicked ? (this.state.validated ? 'success' : 'failure') : ''} 
                    {/*ํด๋ฆญ์„ ํ–ˆ๋Š”๋ฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ 0000์ผ ๊ฒฝ์šฐ success๋ฅผ ๋ฐ˜ํ™˜!*/}
                    onKeyPress={this.onKeyPress}
                />
                <button onClick={this.handleButtonClick}>๊ฒ€์ฆํ•˜๊ธฐ</button>
            </div>
        );
    }
}

export default ValidationSample;

 

์ด ์ฝ”๋“œ์—์„œ ์ธํ’‹์— ํฌ์ปค์Šค๋ฅผ ์ค˜๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

                <input 
                    ref = {(ref) => this.input=ref}
                    type='password'
                    value={this.state.password}
                    onChange={this.handleChange}
                    className={this.state.clicked ? (this.state.validated ? 'success' : 'failure') : ''} 
                    {/*ํด๋ฆญ์„ ํ–ˆ๋Š”๋ฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ 0000์ผ ๊ฒฝ์šฐ success๋ฅผ ๋ฐ˜ํ™˜!*/}
                    onKeyPress={this.onKeyPress}
                />

 

input ํƒœ๊ทธ ์•ˆ์— ref๋ฅผ ๋‹ฌ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค!

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ !!

ref๋ฅผ ๋‹ฌ์•˜์œผ๋ฉด ์„ ์–ธ์„ ํ•ด์ค˜์•ผ๊ฒ ์ฃ ? 

renderํ•จ์ˆ˜ ์ „์— input; ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ๋งˆ์ง€๋ง‰ ๋™์ž‘์ธ handleButtonClick ํ•จ์ˆ˜ ์•ˆ์— this.input.focus();๋กœ ์‹คํ–‰ ์‹œ์ผœ์ค˜๋„๋ฉ๋‹ˆ๋‹ค.

 

handleButtonClick = () => {
        this.setState({
            clicked: true,
            validated: this.state.password === '0000',  //password๊ฐ€ 0000์ผ๋•Œ true๋กœ ๋ฐ˜ํ™˜ํ•˜๋ผ๋Š” ๊ฑด๊ฐ€?
            password: ''
        })
    //๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•
    this.input.focus();
    }

    //์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
    input;
}

 

๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋‹ˆ ref๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์ค„์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์ธ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

๊ทธ ๋ถ€๋ถ„์„ ์›๊ฒฉ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก(?) ๋„์™€์ฃผ๋Š” ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ๋‚˜๋Š” ์ดํ•ด๋ฅผ ํ–ˆ๋‹ค!

์ด๋Ÿฐ ๊ฑธ ๋ณด๋‹ˆ ๊ฒŒ์œผ๋ฅธ ์‚ฌ๋žŒ์ด ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค๋Š” ๊ฒƒ์ด ๋” ์™€๋‹ฟ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค

๋” ํŽธํ•œ ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ๋‚˜๋„ ๊ณ„์† ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค!!

728x90