Post

๐ŸŒŒ React ์ž…๋ฌธ โ…ฆ - React Component์˜ ์ƒ๋ช…์ฃผ๊ธฐ

๐ŸŒŒ React ์ž…๋ฌธ โ…ฆ - React Component์˜ ์ƒ๋ช…์ฃผ๊ธฐ

๐Ÿ“˜ ใ€Ž์†Œํ”Œ์˜ ์ฒ˜์Œ ๋งŒ๋‚œ ๋ฆฌ์•กํŠธใ€๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

React Component์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ž€?

  • React Component๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ๋ Œ๋”๋ง ๋˜๊ณ , ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์ง€๋Š” ์ผ๋ จ์˜ ๊ณผ์ •์„ ๋งํ•œ๋‹ค.

React Component์˜ ์ƒ๋ช…์ฃผ๊ธฐ ์ข…๋ฅ˜

โœ… Mount

  • Component์˜ ์ƒ์„ฑ์ž๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ณผ์ •์ด๋‹ค.
  • ์ƒ์„ฑ์ž์—์„œ๋Š” State๋ฅผ ์ •์˜ํ•˜๊ณ  Component๊ฐ€ ๋ Œ๋”๋ง ๋œ๋‹ค.
  • Class Component์—์„œ๋Š” componentDidMount() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

โœ… Update

  • Component์˜ props๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ State๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ, ๋˜๋Š” forceUpdate() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ•์ œ๋กœ Update๋  ๋•Œ Component๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง ๋œ๋‹ค.
  • ์ด๋•Œ Class Component์—์„œ๋Š” componentDidUpdate() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

โœ… Unmount

  • ํ˜„์žฌ Component๋ฅผ ๋” ์ด์ƒ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜์ง€ ์•Š์„ ๋•Œ Unmount ๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Unmountํ•˜๋ ค๋ฉด ํ˜„์žฌ ๋ Œ๋”๋ง ํ•˜๊ณ  ์žˆ๋Š” Component์˜ ์ƒํƒœ ๊ฐ’์„ ๋นˆ ๊ฐ’์œผ๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.
  • Class Component์—์„œ๋Š” componentWillUnmount() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

์‹ค์Šต

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React from "react";

const style = {
    container: {
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        justifyContent: "flex-start",
        gap: 24,
        padding: "60px 20px",
        background: "linear-gradient(135deg, #fdfbfb 0%, #ebedee 100%)",
        minHeight: "100vh",
        fontFamily: "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
        color: "#2c3e50"
    }
};

class Alert extends React.Component {
    constructor(props) {
        super(props);

        this.state = {};

        this.style = {
            wrapper: {
                width: 800,
                height: 130,
                border: "0",
                borderRadius: 25,
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                backgroundColor: "#ffffff",
                boxShadow: "0 4px 12px rgba(0, 0, 0, 0.1)",
                fontSize: 18,
                fontWeight: "500"
            }
        };
    }

    componentDidMount() {
        console.log(`${this.props.id} componentDidMount() called!`);
    }

    componentDidUpdate() {
        console.log(`${this.props.id} componentDidUpdate() called!`);
    }

    componentWillUnmount() {
        console.log(`${this.props.id} componentWillUnmount() called!`);
    }

    render() {
        return (
            <div style={this.style.wrapper}>
                <span>{this.props.message}</span>
            </div>
        );
    }
}

const reservedAlerts = [
    {
        id: 1,
        message: "์‚ฌ์ดํŠธ์— ๊ธ€์ด ์—…๋กœ๋“œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
    }, 
    {
        id: 2,
        message: "์ ์‹ฌ ์‹์‚ฌ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค."
    },
    {
        id: 3,
        message: "๊ณง ํšŒ์˜๊ฐ€ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค."
    }
];

var timer;

class AlertList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            alerts: []
        };
    }

    componentDidMount() {
        const { alerts } = this.state;
        timer = setInterval(() => {
            if (alerts.length < reservedAlerts.length) {
                const index = alerts.length;
                alerts.push(reservedAlerts[index]);
                this.setState({
                    alerts: alerts
                });
            } else {
                this.setState({
                    alerts: []
                });
                clearInterval(timer);
            }
        }, 1000);
    }

    componentWillUnmount() {
        if (timer) {
            clearInterval(timer);
        }
    }

    render() {
        return (
            <div style={style.container}>
                {this.state.alerts.map((alert) => {
                    return (
                        <Alert 
                            key={alert.id}
                            id={alert.id}
                            message={alert.message}
                        />
                    );
                })}
            </div>
        );
    }
}

export default AlertList;
  • ์•Œ๋ฆผ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ Œ๋”๋ง ํ•˜๊ณ , ์•Œ๋ฆผ์ด ๋ชจ๋‘ ๋ Œ๋”๋ง ๋˜์—ˆ์„ ๊ฒฝ์šฐ ํ™”๋ฉด์„ ๋น„์šด๋‹ค.
  • ์ƒ์„ฑ์ž์—์„œ๋Š” props๋ฅผ React.Component Class์— ์ „๋‹ฌํ•˜๊ณ , alerts State๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  • ํ•ด๋‹น Component๊ฐ€ Mount ๋  ๋•Œ 1์ดˆ๋งˆ๋‹ค ๊ฐฑ์‹  ๋œ ์•Œ๋ฆผ ๋ชฉ๋ก์˜ State ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค.
  • State ๊ฐ’์„ ๋ณ€๊ฒฝํ•จ์œผ๋กœ์จ rendor() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์–ด ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ์•Œ๋ฆผ ๋ชฉ๋ก์ด ๋ชจ๋‘ ๋ Œ๋”๋ง ๋˜์—ˆ์„ ๋•Œ, State ๊ฐ’์— ๋นˆ ๊ฐ’์„ ๋„ฃ์–ด์„œ ํ™”๋ฉด์„ ๋น„์šด๋‹ค.
  • ํ•ด๋‹น Component๊ฐ€ Unmount ๋  ๋•Œ ํƒ€์ด๋จธ์˜ setInterval() ํ•จ์ˆ˜๊ฐ€ ์ค‘๋‹จ๋œ๋‹ค.
  • ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” index.js์˜ <React.StrictMode></React.StrictMode> ๋ผ์ธ์„ ์ œ๊ฑฐํ•ด์•ผ ํ•œ๋‹ค.
  • ํ•ด๋‹น ์ฝ”๋“œ๋Š” Side Effect๋ฅผ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ถ€ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ๋‘ ๋ฒˆ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋งŒ ๋™์ž‘ํ•˜๊ณ  ์‹ค์ œ ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.

  • ์ˆœ์ฐจ์ ์œผ๋กœ ์•Œ๋ฆผ์ด ์ถ”๊ฐ€๋˜๊ณ , ๋งˆ์ง€๋ง‰์— ํ™”๋ฉด์„ ๋น„์šด๋‹ค.

  1. ์ฒซ ๋ฒˆ์งธ ์š”์†Œ Mount
  2. ์ฒซ ๋ฒˆ์งธ ์š”์†Œ Update, ๋‘ ๋ฒˆ์งธ ์š”์†Œ Mount
  3. ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ ์š”์†Œ Update, ์„ธ ๋ฒˆ์งธ ์š”์†Œ Mount
  4. ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ ์š”์†Œ ๋ชจ๋‘ Unmount

ํšŒ๊ณ 

  • Class Component๋ฅผ ํ†ตํ•ด React Component์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ดํ•ด ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • Functional Component์—์„œ๋Š” Hook์„ ์ด์šฉํ•ด ์ƒ๋ช…์ฃผ๊ธฐ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.
This post is licensed under CC BY 4.0 by the author.