๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

[๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ] 4์žฅ - ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง

2020-03-30-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -4์žฅ-์ด๋ฒคํŠธ-ํ•ธ๋“ค๋ง-image-0

์ด ๊ธ€์€ ๊น€๋ฏผ์ค€(velopert)๋‹˜์˜ ๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์„์ฐธ์กฐํ•˜์—ฌ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ์˜ ์ด๋ฒคํŠธ ์‹œ์Šคํ…œ#

์‚ฌ์šฉ์ž๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ DOM ์š”์†Œ๋“ค๊ณผ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ๊ฒƒ์„ ์ด๋ฒคํŠธ(event)๋ผ๊ณ  ํ•œ๋‹ค. ๋ฆฌ์•กํŠธ์˜ ์ด๋ฒคํŠธ ์‹œ์Šคํ…œ์€ ์›น ๋ธŒ๋ผ์šฐ์ €์˜ HTML ์ด๋ฒคํŠธ์™€ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ๋ฒ•์ด ๊ฝค ๋น„์Šทํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ฃผ์˜ํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์‚ฌํ•ญ์ด ์žˆ๋‹ค.


์ด๋ฒคํŠธ ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ ์‚ฌํ•ญ#

  1. ์ด๋ฒคํŠธ ์ด๋ฆ„์€ ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ

    HTML์—์„œ๋Š” onclick ์œผ๋กœ ์ž‘์„ฑํ•˜์ง€๋งŒ ๋ฆฌ์•กํŠธ์—์„œ๋Š” ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ onClick ์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

  2. ์ด๋ฒคํŠธ์—๋Š” ํ•จ์ˆ˜ ํ˜•ํƒœ์˜ ๊ฐ’์„ ์ „๋‹ฌ.

    HTML์—์„œ ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•  ๋•Œ ํฐ๋”ฐ์˜ดํ‘œ ์•ˆ์— ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ๋„ฃ์—ˆ์ง€๋งŒ, ๋ฆฌ์•กํŠธ์—์„œ๋Š” ํ•จ์ˆ˜ ํ˜•ํƒœ์˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ์™ธ๋ถ€์— ๋ฏธ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ „๋‹ฌํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

  3. DOM ์š”์†Œ์—๋งŒ ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    div, button, input, form, span ๋“ฑ์˜ DOM ์š”์†Œ์—๋Š” ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ง์ ‘ ๋งŒ๋“  ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ž์ฒด์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค.

    <MyComponent onClick={doSomething} />

    ์ด ์ฝ”๋“œ๋Š” ์ด๋ฆ„์ด onClick ์ธ props ๋ฅผ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•  ๋ฟ์ด๋‹ค.

    ์ปดํฌ๋„ŒํŠธ์— ์ž์ฒด์ ์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ, ์ „๋‹ฌ๋ฐ›์€ props ๋ฅผ ์ปดํฌ๋„ŒํŠธ๋‚ด๋ถ€์˜ DOM ์ด๋ฒคํŠธ๋กœ ์„ค์ •ํ•  ์ˆ˜๋Š” ์žˆ๋‹ค.

    <div onClick={this.props.onClick}>{/* ... */}</div>

๋ฆฌ์•กํŠธ์—์„œ ์ง€์›ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•ฉ์„ฑ ์ด๋ฒคํŠธ(SyntheticEvent) - React


์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง ์˜ˆ์ œ#

์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ๋ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ#

src ๋””๋ ‰ํ† ๋ฆฌ์— EventPractice.js ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  App ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถˆ๋Ÿฌ์™€ ๋ Œ๋”๋งํ•œ๋‹ค.

EventPractice.js

import React, {Component} from 'react';
class EventPractice extends Component {    render() {        return (            <div>                <h1>Event Practice</h1>            </div>        );    }}
export default EventPractice;

App.js

import React from 'react';import EventPractice from './Practice';
const App = () => {    return <EventPractice />;};
export default App;

onChange ์ด๋ฒคํŠธ ํ•ธ๋“ค๋งํ•˜๊ธฐ#

EventPractice ์ปดํฌ๋„ŒํŠธ์— input ์š”์†Œ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ์ฝ”๋“œ์™€ ํ•ด๋‹น ์š”์†Œ์— onChange ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="message"                    placeholder="type something"                    onChange={(e) => {                        console.log(e);                    }}                />            </div>        );    }}
export default EventPractice;

yarn start ๋˜๋Š” npm start ๋กœ ๋ฆฌ์•กํŠธ ์•ฑ์„ ์‹คํ–‰ํ•˜๊ณ , F12 ๋ฅผ ๋ˆŒ๋Ÿฌ ์ฝ˜์†” ์ฐฝ์„ํ‚จํ›„ input ์š”์†Œ์— ์•„๋ฌด ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•ด๋ณธ๋‹ค.

2020-03-30-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -4์žฅ-์ด๋ฒคํŠธ-ํ•ธ๋“ค๋ง-image-1


์ฝ˜์†”์— SyntheticzEvent ๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๊ฐ์ฒด๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ๋„ค์ดํ‹ฐ๋ธŒ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์‹ธ๋Š” ๊ฐ์ฒด๋กœ, ์ด๋ฒคํŠธ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ(์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” e )๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋„ค์ดํ‹ฐ๋ธŒ ์ด๋ฒคํŠธ์™€ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ, ์ˆœ์ˆ˜ JS์—์„œ HTML ์ด๋ฒคํŠธ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ์™€ ๋˜‘๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€๋งŒ, ๋„ค์ดํ‹ฐ๋ธŒ ์ด๋ฒคํŠธ์™€ ๋‹ฌ๋ฆฌ ์ด๋ฒคํŠธ๊ฐ€ ๋๋‚˜๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ์ดˆ๊ธฐํ™” ๋˜๋ฏ€๋กœ ์ •๋ณด๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•œ๋‹ค๋ฉด e.persist() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.


์ด์ œ state ์— input ๊ฐ’์„ ๋‹ด์•„๋ณด๋„๋ก ํ•œ๋‹ค. state ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this.setState ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ state ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    state = {        message: '',    };
    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="message"                    placeholder="type something"                    value={this.state.message}                    onChange={(e) => {                        this.setState({                            message: e.target.value,                        });                    }}                />            </div>        );    }}
export default EventPractice;

์ธํ’‹์— ์•„๋ฌด๊ฑฐ๋‚˜ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด state ์— ํ…์ŠคํŠธ๋ฅผ ์ž˜ ๋‹ด์€๊ฒƒ์ด๋‹ค.

์ด์ œ input ์š”์†Œ ์•„๋ž˜์— button ์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ํด๋ฆญ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ˜„์žฌ input ์— ๋‹ด๊ธด ํ…์ŠคํŠธ๋ฅผ ๋„์šด ํ›„ ๊ณต๋ฐฑ์œผ๋กœ ์„ค์ •ํ•˜๋„๋ก ํ•œ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    state = {        message: '',    };
    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="message"                    placeholder="type something"                    value={this.state.message}                    onChange={(e) => {                        this.setState({                            message: e.target.value,                        });                    }}                />                <button                    onClick={() => {                        alert(this.state.message);                        this.setState({                            message: '',                        });                    }}                >                    Submit                </button>            </div>        );    }}
export default EventPractice;

2020-03-30-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -4์žฅ-์ด๋ฒคํŠธ-ํ•ธ๋“ค๋ง-image-2


์ž„์˜ ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ#

ํ˜„์žฌ์˜ ์ฝ”๋“œ๋Š” ์ด๋ฒคํŠธ์— ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ๋ Œ๋”๋ง ๋ฉ”์†Œ๋“œ ๋‚ด๋ถ€์— ๋งŒ๋“ค์–ด์„œ ์ „๋‹ฌํ•ด์ฃผ๊ณ  ์žˆ๋‹ค. ์ด ๋ฐฉ๋ฒ• ๋Œ€์‹  ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ์ „๋‹ฌํ•˜๋ฉด ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    state = {        message: '',    };
    constructor(props) {        super(props);        this.handleChange = this.handleChange.bind(this);        this.handleClick = this.handleClick.bind(this);    }
    handleChange(e) {        this.setState({            message: e.target.value,        });    }
    handleClick() {        alert(this.state.message);        this.setState({            message: '',        });    }
    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="message"                    placeholder="type something"                    value={this.state.message}                    onChange={this.handleChange}                />                <button onClick={this.handleClick}>Submit</button>            </div>        );    }}
export default EventPractice;

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ this ๋ฐ”์ธ๋”ฉ์ด๋‹ค. this๋Š” ํ•จ์ˆ˜๊ฐ€ ์–ด๋””์„œ ํ˜ธ์ถœ๋˜๋Š”์ง€์— ๋”ฐ๋ผ๊ฒฐ์ •๋œ๋‹ค. ํด๋ž˜์Šค์˜ ์ž„์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํŠน์ • HTML ์š”์†Œ์˜ ์ด๋ฒคํŠธ๋กœ ๋“ฑ๋ก๋˜๋Š” ๊ณผ์ •์—์„œ ๋ฉ”์†Œ๋“œ์™€ this์˜ ๊ด€๊ณ„๊ฐ€ ๋Š์–ด์ง€๊ฒŒ ๋œ๋‹ค.

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

ํ•˜์ง€๋งŒ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ public class fields ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด, ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ this๋Š” ๋ถ€๋ชจ ํ•จ์ˆ˜์˜ this๋ฅผ ์ƒ์†๋ฐ›๋Š”๋ฐ JS์—์„œ ํด๋ž˜์Šค๋Š” ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— this๋Š” ์ปดํฌ๋„ŒํŠธ ์ž์‹ ์ด ๋˜๋ฏ€๋กœ bind(this)๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    state = {        message: '',    };
    handleChange = (e) => {        this.setState({            message: e.target.value,        });    };
    handleClick = () => {        alert(this.state.message);        this.setState({            message: '',        });    };
    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="message"                    placeholder="type something"                    value={this.state.message}                    onChange={this.handleChange}                />                <button onClick={this.handleClick}>Submit</button>            </div>        );    }}
export default EventPractice;

์ด ๋ฌธ๋ฒ•์€ ์‹คํ—˜์ ์ธ ๋ฌธ๋ฒ•์œผ๋กœ ๋ฐ”๋ฒจ์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ Create-React-App ์œผ๋กœ ๋งŒ๋“  ํ”„๋กœ์ ํŠธ๋Š” ์ด ๋ฌธ๋ฒ•์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.


input ์—ฌ๋Ÿฌ ๊ฐœ ๋‹ค๋ฃจ๊ธฐ#

์ด์ œ input ๊ฐ’์„ state ์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ input ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, event ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜๋ฉด๋” ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

onChange ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ e.target.name ์€ ํ•ด๋‹น input์˜ name์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ด๊ฐ’์„ ์ด์šฉํ•ด state๋ฅผ ์„ค์ •ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    state = {        username: '',        message: '',    };
    handleChange = (e) => {        this.setState({            [e.target.name]: e.target.value,        });    };
    handleClick = () => {        alert(this.state.username + ': ' + this.state.message);        this.setState({            username: '',            message: '',        });    };
    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="username"                    placeholder="User name"                    value={this.state.username}                    onChange={this.handleChange}                />                <input                    type="text"                    name="message"                    placeholder="type something"                    value={this.state.message}                    onChange={this.handleChange}                />                <button onClick={this.handleClick}>Submit</button>            </div>        );    }}
export default EventPractice;

2020-03-30-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -4์žฅ-์ด๋ฒคํŠธ-ํ•ธ๋“ค๋ง-image-3


handleChange = (e) => {    this.setState({        [e.target.name]: e.target.value,    });};

์ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด e.target.name ์„ [] ๋กœ ๊ฐ์‹ธ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด ์•ˆ์—์„œ key๋ฅผ [] ๋กœ ๊ฐ์‹ธ๋ฉด ๊ทธ ์•ˆ์— ๋„ฃ์€ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹ค์ œ ๊ฐ’์ด key ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.


onKeyPress ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง#

ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” KeyPress ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•ด๋ณธ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธํ’‹์—์„œ Enter ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ handleCick ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

import React, {Component} from 'react';
class EventPractice extends Component {    state = {        username: '',        message: '',    };
    handleChange = (e) => {        this.setState({            [e.target.name]: e.target.value,        });    };
    handleClick = () => {        alert(this.state.username + ': ' + this.state.message);        this.setState({            username: '',            message: '',        });    };
    handleKeyPress = (e) => {        if (e.key === 'Enter') {            this.handleClick();        }    };
    render() {        return (            <div>                <h1>Event Practice</h1>                <input                    type="text"                    name="username"                    placeholder="User name"                    value={this.state.username}                    onChange={this.handleChange}                />                <input                    type="text"                    name="message"                    placeholder="type something"                    value={this.state.message}                    onChange={this.handleChange}                    onKeyPress={this.handleKeyPress}                />                <button onClick={this.handleClick}>Submit</button>            </div>        );    }}
export default EventPractice;

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ธฐ#

์ด์ œ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ–ˆ๋˜ EventPractice ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ”๊พธ์–ด๋ณธ๋‹ค.

EventPractice.js

import React, {useState} from 'react';
const EventPractice = () => {    const [username, setUsername] = useState('');    const [message, setMessage] = useState('');    const onChangeUsername = (e) => setUsername(e.target.value);    const onChangeMessage = (e) => setMessage(e.target.value);    const onClick = () => {        alert(username + ': ' + message);        setUsername('');        setMessage('');    };    const onKeyPress = (e) => {        if (e.key === 'Enter') {            onClick();        }    };
    return (        <div>            <h1>Event Practice</h1>            <input                type="text"                name="username"                placeholder="User name"                value={username}                onChange={onChangeUsername}            />            <input                type="text"                name="message"                placeholder="type something"                value={message}                onChange={onChangeMessage}                onKeyPress={onKeyPress}            />            <button onClick={onClick}>Submit</button>        </div>    );};
export default EventPractice;

๊ธฐ๋Šฅ์ด ์ด์ „๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณธ๋‹ค.

์œ„์˜ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์ฝ”๋“œ์—์„œ๋Š” e.target.name ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  onChange ๊ด€๋ จํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. ํ•˜์ง€๋งŒ input์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ๋ถˆํŽธํ•ด์ง€๋ฏ€๋กœ, e.target.name ์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ์ˆ˜ ์žˆ๋‹ค.

import React, {useState} from 'react';
const EventPractice = () => {    const [form, setForm] = useState({        username: '',        message: '',    });    const {username, message} = form;    const onChange = (e) => {        const nextForm = {            ...form,            [e.target.name]: e.target.value,        };        setForm(nextForm);    };    const onClick = () => {        alert(username + ': ' + message);        setForm({            username: '',            message: '',        });    };    const onKeyPress = (e) => {        if (e.key === 'Enter') {            onClick();        }    };
    return (        <div>            <h1>Event Practice</h1>            <input                type="text"                name="username"                placeholder="User name"                value={username}                onChange={onChange}            />            <input                type="text"                name="message"                placeholder="type something"                value={message}                onChange={onChange}                onKeyPress={onKeyPress}            />            <button onClick={onClick}>Submit</button>        </div>    );};
export default EventPractice;

e.target.name ๊ฐ’์„ ํ™œ์šฉํ•˜๋ ค๋ฉด useState ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ input ๊ฐ’๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ •๋ฆฌ#

๋ฆฌ์•กํŠธ์—์„œ ์ด๋ฒคํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์€ ์ˆœ์ˆ˜ JS ๋˜๋Š” jQuery๋ฅผ ์‚ฌ์šฉํ•œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ์ด๋ฒคํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๊ธฐ์กด HTML DOM Event๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๋ฆฌ์•กํŠธ์˜์ปดํฌ๋„ŒํŠธ ์ด๋ฒคํŠธ๋„ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ™์ด ์ฝ๊ธฐ#

์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ - React

https://medium.com/@wongni/%EB%A6%AC%EC%95%A1%ED%8A%B8%EC%97%90%EC%84%9C-bind-this-%EB%B9%BC-%EB%B2%84%EB%A6%AC%EA%B8%B0-dfb0bbf7bef0