🎉 berenickt 블로그에 온 걸 환영합니다. 🎉
Front
03-컴포넌트

1. Components란?

  • Components = 독립되고 재사용가능한 단위 모듈
    • e.g. 유저가 사용하는 시스템에 대한 조작장치
      • page : 모든 구성요소를 포함하는 가장 큰 단위로 single template와 multi-page template로 구분
      • dialog : 대화상자
      • header bar : 상단에 위치하면서 제목을 표시하고 페이지 이동과 관련된 버튼이 위치
      • footer bar : 페이지 하단에 위치하면서 버튼이 위치
      • navbars : 일반적으로 header bar나 footer bar 내에 위치하면서 특정한 페이지로 이동하는 메뉴의 역할
      • button : 버튼
      • form : 사용자로부터 데이터를 입력받는 컨트롤
      • list view : 여러개의 항목을 열거하는데 사용되는 컨트롤
    • e.g. 최신식 음식점
      • 키오스크
      • 음식 만드는 인간
      • 서빙하는 인간
      • 손님앉을 자리
      • 식재료 보관함 등등
  • React = Components들을 조합한 것들
  • 공식 문서

2. Component를 선언하는 2가지 방법

컴포넌트를 선언하는 방식은 두 가지입니다.

  1. 클래스형 컴포넌트 : 옛날에 나온 거
  2. 함수형 컴포넌트 : 권장하는 방식

2.1 클래스형 컴포넌트

  • React 초창기 방식, 이제 안씀
  • 함수형 컴포넌트과 차이점
    • 뒤에서 배울 state기능 및 라이프사이클 기능을 사용 가능
    • 임의 메서드를 정의할 수 있음
    • render() 함수가 꼭 있어야 함
1
// 이렇게 class 문법을 사용해서 선언함
2
class App extends Component {
3
componentDidMount() {
4
// App 컴포넌트가 로드되고나서 실행할 코드
5
}
6
componentDidUpdate() {
7
// App 컴포넌트가 업데이트 되고나서 실행할 코드
8
}
9
componentWillUnmount() {
10
// App 컴포넌트가 삭제되기전에 실행할 코드
11
}
12
render() {
13
const name = 'react'
14
return <div className="react">{name}</div>
15
}
16
}

2.2 함수형 컴포넌트

  • React에서 권장하는 방식
1
import React from 'react'
2
import './App.css'
3
4
function App() {
5
const name = '리액트'
6
return <div className="react">{name}</div>
7
}
8
9
export default App
  • 장점
    • 클래스형 컴포넌트보다 보기 좋음
    • 메모리 자원도 클래스형 컴포넌트보다 덜 사용함
  • 단점
    • state와 라이프사이클 API의 사용 불가능,
    • 위 단점은 리액트 v16.8 업데이트 이후 Hooks라는 기능이 도입되면서 해결됨

2.3 컴포넌트 생성 스니펫

VSCode 확장 Reacts Code snippets을 사용하거나, 또는 직접 global.code-snippets.json을 작성해도 됩니다. Ctrl + Shift + P를 치고 global.code-snippets를 찾습니다.

  • 스니펫(snippet) = 코드 빨리 생성하기 위해 템플릿(=틀)을 만들어둔 약어 모음집
1
{
2
// Place your snippets for javascriptreact here. Each snippet is defined under a snippet name and has a prefix, body and
3
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
4
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
5
// same ids are connected.
6
// Example:
7
// "Print to console": {
8
// "prefix": "log",
9
// "body": [
10
// "console.log('$1');",
11
// "$2"
12
// ],
13
// "description": "Log output to console"
14
// }
15
"reactFunction": {
16
"prefix": "rfc",
17
"body": "import React from 'react';\n\nexport default function ${1:${TM_FILENAME_BASE}}() {\n\treturn (\n\t\t<div>\n\t\t\t\n\t\t</div>\n\t);\n}\n\n",
18
"description": "Creates a React Function component"
19
},
20
"reactStatelessImplicitReturn": {
21
"prefix": "rsi",
22
"body": "import React from 'react';\n\nexport const ${1:${TM_FILENAME_BASE}} = (props) => (\n\t\t\t$0\n\t);",
23
"description": "Creates a React Function component"
24
},
25
"Import Module CSS": {
26
"prefix": "icss",
27
"body": ["import styles from './$TM_FILENAME_BASE.module.css'"],
28
"description": "Import PostCSS"
29
},
30
"ClassName": {
31
"prefix": "cn",
32
"body": ["className={styles.$1}"],
33
"description": "Adding className"
34
}
35
}
  • rfc : react 컴포넌트 생성
  • rsi
  • icss : postCSS 추가
  • cn : className 추가

이제 빈 폴더에 위 명령어 입력하고 Shift + Enter하면 스니펫(코드 틀)이 자동 생성됨