Post

🌌 React μž…λ¬Έ β…€ - Component

🌌 React μž…λ¬Έ β…€ - Component

πŸ“˜ γ€Žμ†Œν”Œμ˜ 처음 λ§Œλ‚œ λ¦¬μ•‘νŠΈγ€λ₯Ό 읽고 μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.

Componentλž€?

  • ComponentλŠ” propsλ₯Ό 톡해 속성을 μž…λ ₯ν•˜μ—¬ React Elementλ₯Ό 좜λ ₯ν•˜λŠ” Javascript ν•¨μˆ˜λ‘œ, μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ κ΅¬μ„±ν•˜λŠ” μž¬μ‚¬μš© κ°€λŠ₯ν•œ λΆ€ν’ˆμ΄λΌκ³  ν•  수 μžˆλ‹€.
  • ν•˜λ‚˜μ˜ ComponentλŠ” μ—¬λŸ¬ 개의 Component둜 ꡬ성할 수 μžˆλ‹€.
  • Component둜 λΆ„λ¦¬ν•¨μœΌλ‘œμ¨ μ½”λ“œμ˜ 가독성을 확보할 수 있고, μ½”λ“œ 양을 μ€„μž„μœΌλ‘œμ¨ 개발 및 μœ μ§€ 보수 μ‹œκ°„μ„ μ ˆκ°ν•  수 μžˆλ‹€.

propsλž€?

  • ComponentλŠ” ν•˜λ‚˜μ˜ 객체이며, propsλŠ” 객체의 속성에 ν•΄λ‹Ήν•œλ‹€.
  • κ°€λ Ή μ†μ„±λ§Œ λ‹€λ₯Έ React Elementλ₯Ό μΆ”κ°€ν•˜κ³  μ‹Άλ‹€λ©΄ 동일 Componentλ₯Ό μž¬μ‚¬μš©ν•˜μ—¬ props만 λ‹€λ₯΄κ²Œ μ£Όμž…ν•˜λ©΄ λœλ‹€.
  • propsλŠ” 읽기 μ „μš©μ΄μ–΄μ„œ 값을 μˆ˜μ •ν•  수 μ—†λ‹€.
  • React Elementλ₯Ό μƒμ„±ν•˜λŠ” 도쀑에 propsκ°€ λ°”λ€Œμ–΄ 버리면 Reactκ°€ μ œλŒ€λ‘œ λ™μž‘ν•˜μ§€ λͺ»ν•˜κΈ° λ•Œλ¬Έμ— 읽기 μ „μš©μ΄λΌλŠ” νŠΉμ§•μ„ κ°–λŠ”λ‹€.
  • 동일 props에 λŒ€ν•΄μ„œλŠ” 항상 같은 κ²°κ³Όλ₯Ό 보여주어야 ν•œλ‹€.
  • λ‹€λ₯Έ propsλ₯Ό κ°€μ§„ React Elementλ₯Ό λ Œλ”λ§ ν•˜κ³  μ‹ΆμœΌλ©΄ μƒˆλ‘œμš΄ 값을 Component에 μ „λ‹¬ν•˜λ©΄ λ˜λŠ”λ°, 이 κ³Όμ •μ—μ„œ React Elementκ°€ λ Œλ”λ§ λœλ‹€.
1
2
3
4
5
6
7
8
9
function App(props) {
	return (
		<Profile
			name="brobro332"
			introduction="μ•ˆλ…•ν•˜μ„Έμš”!"
			viewCount={20}
		/>
	);
}
  • Profile Component에 name, introduction, viewCount 속성을 μ „λ‹¬ν•˜μ—¬ React Elementλ₯Ό μƒμ„±ν•˜λŠ” μ˜ˆμ œμ΄λ‹€.
  • λ¬Έμžμ—΄ μ΄μ™Έμ˜ μ •μˆ˜, λ³€μˆ˜, Component 등을 μ‚¬μš©ν•  경우 μ€‘κ΄„ν˜Έλ‘œ 감싸야 ν•œλ‹€.
  • λ¬Έμžμ—΄λ„ μ€‘κ΄„ν˜Έλ‘œ 감싸도 상관없닀.
  • μ€‘κ΄„ν˜Έλ₯Ό μ‚¬μš©ν•˜λ©΄ Javascript μ½”λ“œλ₯Ό μ‚¬μš©ν•œλ‹€κ³  μ„ μ–Έν•˜λŠ” 것과 κ°™λ‹€.

Component μ’…λ₯˜

βœ… Class Component

1
2
3
4
5
class Welcome extends React.Component {
	render() {
		return <h1>μ•ˆλ…•, {this.name}</h1>;
	}
}
  • Class ComponentλŠ” Javascript ES6의 Classλ₯Ό μ‚¬μš©ν•΄μ„œ λ§Œλ“€μ–΄μ§„ ν˜•νƒœμ΄λ‹€.
  • React의 λͺ¨λ“  Class ComponentλŠ” React.Componentλ₯Ό 상속 λ°›μ•„μ„œ λ§Œλ“ λ‹€.

βœ… Functional Component

  • 초기 λ²„μ „μ—λŠ” Class Componentλ₯Ό 주둜 μ‚¬μš©ν•˜μ˜€μœΌλ‚˜ λΆˆνŽΈν•˜λ‹€λŠ” 의견이 λ‹€μˆ˜μ˜€λ‹€.
  • κ°œμ„ ν•˜λŠ” κ³Όμ •μ—μ„œ Hookμ΄λΌλŠ” 것이 κ°œλ°œλ˜μ—ˆλŠ”λ° React의 μ£Όλ ₯ κΈ°λŠ₯이기 λ•Œλ¬Έμ— μ•žμœΌλ‘œλŠ” 주둜 Functional Componentλ₯Ό μ‚¬μš©ν•˜κ²Œ 될 것이닀.

Component νŠΉμ§•

βœ… Component 이름은 항상 λŒ€λ¬Έμžλ‘œ μ‹œμž‘ν•΄μ•Ό ν•œλ‹€.

  • ReactλŠ” μ†Œλ¬Έμžλ‘œ μ‹œμž‘ν•˜λŠ” Componentλ₯Ό DOM νƒœκ·Έλ‘œ μΈμ‹ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
  • 예λ₯Ό λ“€μ–΄ <div>λŠ” λ‚΄μž₯ Component이며 "div" λ¬Έμžμ—΄λ‘œ React.createElement()에 μ „λ‹¬λœλ‹€.
  • <Foo />와 같이 λŒ€λ¬Έμžλ‘œ μ‹œμž‘ν•˜λŠ” κ²½μš°λŠ” "Foo" λ¬Έμžμ—΄λ‘œ React.createElement()에 μ „λ‹¬λ˜λŠ” 건 λ™μΌν•˜μ§€λ§Œ λ‚΄μž₯ Componentκ°€ μ•„λ‹Œ Javascript 파일 λ‚΄μ—μ„œ μ‚¬μš©μžκ°€ μ •μ˜ν–ˆκ±°λ‚˜ importν•œ Componentλ₯Ό 가리킨닀.

βœ… Component λ Œλ”λ§

  • Element λ Œλ”λ§κ³Ό λ™μΌν•˜κ²Œ root.render()에 인자둜 Componentλ₯Ό μ „λ‹¬ν•˜λ©΄ λœλ‹€.

βœ… Component ν•©μ„±

  • μ—¬λŸ¬ 개의 Componentλ₯Ό ν•©μ³μ„œ ν•˜λ‚˜μ˜ Componentλ₯Ό λ§Œλ“œλŠ” 것을 μ˜λ―Έν•œλ‹€.

βœ… Component μΆ”μΆœ

  • 큰 Componentμ—μ„œ 일뢀λ₯Ό μΆ”μΆœν•΄μ„œ μƒˆλ‘œμš΄ Componentλ₯Ό λ§Œλ“œλŠ” 것이닀.
  • κΈ°λŠ₯ λ‹¨μœ„λ‘œ κ΅¬λΆ„ν•˜κ³  λ‚˜μ€‘μ— κ³§λ°”λ‘œ μž¬μ‚¬μš©ν•  수 μžˆλŠ” ν˜•νƒœλ‘œ μΆ”μΆœν•˜λŠ” 것이 μ’‹λ‹€.
  • Component λ‚΄μ˜ λ°˜λ³΅λ˜κ±°λ‚˜ κΈ°λŠ₯으둜 κ΅¬λΆ„λ˜λŠ” μ½”λ“œλ₯Ό ν•¨μˆ˜λ‘œ μΆ”μƒν™”ν•˜λŠ” 것이닀.

μ‹€μŠ΅

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
const style = {
    wrapper: {
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        gap: 20,
        padding: 20,
        backgroundColor: '#f4f4f9',
        minHeight: '100vh'
    },
    card: {
        width: 600,
        padding: '20px 30px',
        borderRadius: 20,
        boxShadow: '0 10px 20px rgba(0, 0, 0, 0.1)',
        backgroundColor: '#ffffff',
        transition: 'all 0.3s ease',
        cursor: 'default'
    },
    title: {
        margin: 0,
        fontSize: 20,
        fontWeight: 'bold',
        color: '#2c3e50'
    },
    description: {
        marginTop: 8,
        fontSize: 16,
        color: '#555'
    }
};

function Project(props) {
    return (
        <div style={style.card}>
            <h3 style={style.title}>βœ… {props.name}</h3>
            <h4 style={style.description}>{props.description}</h4>
        </div>
    );
}

const projects = [
    {
        name: "java-co-working project",
        description: "ν˜‘μ—… μžλ°” ν”„λ‘œμ νŠΈ"
    },
    {
        name: "java-schedule-project",
        description: "μŠ€μΌ€μ₯΄ μžλ°” ν”„λ‘œμ νŠΈ"
    },
    {
        name: "python-ai-project",
        description: "AI 파이썬 ν”„λ‘œμ νŠΈ"
    }
];

function ProjectList() {
    return (
        <div style={style.wrapper}>
            {
	            projects.map((project) => (
	                <Project
	                    key={project.name}
	                    name={project.name}
	                    description={project.description}
	                />
	            ))
            }
        </div>
    );
}

export default ProjectList;
  • ProjectList Component λŠ” Project Component λ₯Ό μžμ‹ Component 둜 ν¬ν•¨ν•˜κ³  μžˆλ‹€.

회고

  • React ComponentλŠ” λ™μΌν•œ propsλ₯Ό λ°›μœΌλ©΄ 항상 λ™μΌν•œ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•΄μ•Ό ν•˜λ©°, μ΄λŸ¬ν•œ νŠΉμ„±μ„ pureν•˜λ‹€κ³  ν‘œν˜„ν•œλ‹€.
  • μ΄λŠ” React의 Elementκ°€ λΆˆλ³€ 객체이기 λ•Œλ¬Έμ— κ°€λŠ₯ν•œ 것이며, λ”°λΌμ„œ Component의 props도 읽기 μ „μš©μœΌλ‘œ μ·¨κΈ‰λœλ‹€λŠ” 사싀을 이해 ν•  수 μžˆλ‹€.
This post is licensed under CC BY 4.0 by the author.