Frontend/React

React | event (์ด๋ฒคํŠธ) ์— ๋Œ€ํ•ด์„œ

์ด์ œํฌ 2023. 11. 18. 15:00

 


event (์ด๋ฒคํŠธ)

  • ์นด๋ฉœ ์ผ€์ด์Šค๋กœ ์ž‘์„ฑ๋˜๋ฉฐ, html ์—์„œ onclick์€ React์—์„œ onClick์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.
  • ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์ž‘์„ฑ๋˜๋ฉฐ, ์ต๋ช…ํ•จ์ˆ˜๋กœ๋„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ต๋ช…ํ•จ์ˆ˜ ๋‚ด์—์„  this๊ฐ€ ์•„๋ฌด๊ฒƒ๋„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š์œผ๋ฏ€๋กœ bind ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ this๋ฅผ ์ง€์ •ํ•ด์ค€๋‹ค.
import React, { Component } from 'react';

class MyComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            buttonName = '์ €์žฅ'
        };
    }

    handleClick() {
      console.log(this.state.buttonName, '๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
    }

    render() {
        return (
            <button onClick={this.handleClick}>ํด๋ฆญ</button>
            <button onClick={function() {
                console.log(this.state.buttonName, '๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
            }.bind(this)}
            >ํด๋ฆญ</button>
        );
    }
}

์ปดํฌ๋„ŒํŠธ ๊ฐ„ ์ด๋ฒคํŠธ ์‚ฌ์šฉ

  • ์ž์‹ -> ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ณ ์ž ํ• ๋•Œ event๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • props์˜ ์†์„ฑ์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋ฒคํŠธ ์ต๋ช…ํ•จ์ˆ˜์˜ ์ฒซ๋ฒˆ์งธ default ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ๋„˜์–ด์˜จ๋‹ค.
  • bind ํ•จ์ˆ˜ this ํŒŒ๋ผ๋ฏธํ„ฐ ์™ธ์— ๋‹ค๋ฅธ ๊ฑธ ์ง€์ •ํ•˜๋ฉด default ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ด๋ฒคํŠธ๋Š” ๋’ค๋กœ ๋ฐ€๋ ค๋‚œ๋‹ค.
// ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ
import React, { Component } from 'react';
import ChildComponent './ChildComponent';

class ParentComponent extends Component {
	constructor(props) {
    	super(props);
        this.state = {
        	title = '';
        }
    }

    render() {
        return (
            <ChildComponent onCustomEvent={function(_title) {
            	this.state.title = _title;
                console.log(_title);
            }.bind(this)}></ChildComponent>
        );
    }
}
// ์ž์‹ ์ปดํฌ๋„ŒํŠธ
import React, { Component } from 'react';

class ChildComponent extends Component {
    constructor() {
        super(props);
        this.state = {
            contents: [
                { id: 1, title: 'HTML' },
                { id: 2, title: 'CSS' },
                { id: 3, title: 'JavaScript' }
            ]
        }
    }
    
    render() {
    	var i = 0;
        var lists = [];
        
        while (i < this.state.contents.length) {
            var content = this.state.contents[i];
            lists.push(
                <li key={content.id}>
                    <a onClick={
                        function(_title, e) {
                            e.preventDefault();
                            this.props.onCustomEvent(_title);
                        }.bind(this, content.title)
                    }>{title}</a>
                </li>
            );
            i = i+1;
        }
        
        return (
            <ul>
                {lists}
            </ul>
        );
    }
}

export default ChildComponent;
๋ฐ˜์‘ํ˜•