Search

React ์†Œ๊ฐœ

React

Meta(FaceBook)์—์„œ ๊ฐœ๋ฐœํ•œ JavaScript ๊ธฐ๋ฐ˜ UI ๊ฐœ๋ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
React๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ•œ JavaScript ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. React๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌ์ถ•๊ณผ ๊ด€๋ฆฌ๋ฅผ ๋„์™€์ฃผ๋Š” ์„ ์–ธ์ ์ด๊ณ  ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. React๋Š” ๋‹จ์ผ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(SPA) ๋ฐ ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํŠนํžˆ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋ฉฐ, ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
React๋Š” 2011๋…„์— ํŽ˜์ด์Šค๋ถ์—์„œ ์ฒ˜์Œ์œผ๋กœ ๋„์ž…๋˜์—ˆ๊ณ , 2013๋…„์— ์˜คํ”ˆ์†Œ์Šค๋กœ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ดํ›„๋กœ๋„ React ์ปค๋ฎค๋‹ˆํ‹ฐ๋Š” ๊ณ„์† ์„ฑ์žฅํ•˜๊ณ  ๋ฐœ์ „ํ•˜๋ฉฐ, ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„๋Œ€์ ์ด๊ณ  ํšจ์œจ์ ์ธ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

React ์ฐฝ์‹œ์ž

React๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœ๋˜์—ˆ์œผ๋ฉฐ, React๋ฅผ ๋งŒ๋“  ํŒ€ ์ค‘ ์ฃผ์š”ํ•œ ์ธ๋ฌผ ์ค‘ ํ•œ ๋ช…์€ Jordan Walke์ž…๋‹ˆ๋‹ค. Jordan Walke๋Š” React๋ฅผ ์ดˆ๊ธฐ์— ๊ฐœ๋ฐœํ•˜๊ณ  ๊ทธ ์•„์ด๋””์–ด๋ฅผ ๊ตฌ์ƒํ•œ ์ฃผ์š” ๊ฐœ๋ฐœ์ž ์ค‘ ํ•˜๋‚˜๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
Jordan Walke๋Š” React์˜ ์ดˆ๊ธฐ ๋ฒ„์ „์„ ๋งŒ๋“ค๋ฉด์„œ JSX(JavaScript XML) ๋ฌธ๋ฒ•์„ ๋„์ž…ํ•˜๊ณ , React์˜ ํ•ต์‹ฌ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜์ธ ๊ฐ€์ƒ DOM(Virtual DOM)์„ ๋„์ž…ํ•˜๋Š” ๋“ฑ์˜ ๊ธฐ์ˆ ์ ์ธ ๊ฒฐ์ •์„ ๋‚ด๋ฆฐ ์ธ๋ฌผ ์ค‘ ํ•˜๋‚˜๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

React ํ•ต์‹ฌ ๊ฐœ๋…

React ๋Š” ๊ฐ€์ƒ DOM(Virtual DOM)์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ(attribute) ๋˜๋Š” ์ƒํƒœ(state)์˜ ๋ณ€ํ™”์— ๋ฐ˜์‘ํ•˜์—ฌ UI๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ตฌ์กฐ์ด๋‹ค.

Reconciliation (์žฌ์กฐ์ •)

1.
๊ฐ€์ƒ DOM ์ƒ์„ฑ : ์ƒํƒœ๋‚˜ ์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜๋ฉด React๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒˆ๋กœ์šด ๊ฐ€์ƒ DOM์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ€์ƒ DOM์€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•œ ์ปดํฌ๋„ŒํŠธ์˜ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
2.
์ด์ „ ๊ฐ€์ƒ DOM๊ณผ ๋น„๊ต : ์ƒˆ๋กœ์šด ๊ฐ€์ƒ DOM์€ ์ด์ „ ๊ฐ€์ƒ DOM๊ณผ ๋น„๊ต๋ฉ๋‹ˆ๋‹ค. ์ด ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„์ด ์‹๋ณ„๋˜๊ณ , ์‹ค์ œ DOM์— ์ ์šฉํ•ด์•ผ ํ•  ์ตœ์†Œํ•œ์˜ ์—…๋ฐ์ดํŠธ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.
3.
์‹ค์ œ DOM ์—…๋ฐ์ดํŠธ : ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„์ด ์‹๋ณ„๋˜๋ฉด React๋Š” ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์‹ค์ œ DOM์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ตœ์†Œํ•œ์˜ ์กฐ์ž‘๋งŒ์ด ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

1. ๊ฐ€์ƒ DOM ์ƒ์„ฑ

2. ์ด์ „ ๊ฐ€์ƒ DOM๊ณผ ๋น„๊ต

3. ์‹ค์ œ DOM ์—…๋ฐ์ดํŠธ

์ƒ๊ฐํ•ด๋ณด๊ธฐ

์ƒํƒœ ๋ณ€๊ฒฝ์„ ๊ฐ์ง€ํ•ด์„œ ๋ชจ๋“  ๊ตฌ์กฐ๋ฅผ ๋‹ค์‹œ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋น ๋ฅผ๊นŒ? ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋งŒ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์ด ๋น ๋ฅผ๊นŒ?
Reconciliation (์žฌ์กฐ์ •)์„ ํ†ตํ•ด React๋Š” ํšจ์œจ์ ์œผ๋กœ UI๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” React์˜ ์„ฑ๋Šฅ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋กœ, ํ™”๋ฉด ์ „์ฒด๋ฅผ ๋‹ค์‹œ ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋งŒ์„ ๊ฐ์ง€ํ•˜์—ฌ ์—…๋ฐ์ดํŠธํ•˜๋ฏ€๋กœ์จ ๋น ๋ฅธ ํ™”๋ฉด ๊ฐฑ์‹ ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์š” ๊ฐœ๋…

1.
์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ : React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์ž‘๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์ฒด์ ์œผ๋กœ ์ƒํƒœ(state)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค.
2.
๊ฐ€์ƒ DOM (Virtual DOM) : React๋Š” ๊ฐ€์ƒ DOM์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์— ๋ Œ๋”๋ง๋˜๊ธฐ ์ „์— ๊ฐ€์ƒ์œผ๋กœ ๋ชจ๋“  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํšจ์œจ์ ์œผ๋กœ ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜๊ณ  ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
3.
JSX (JavaScript XML) : React์—์„œ๋Š” JSX๋ผ๋Š” ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ JavaScript ์ฝ”๋“œ ์•ˆ์—์„œ XML๊ณผ ์œ ์‚ฌํ•œ ๋ฌธ๋ฒ•์œผ๋กœ UI๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JSX๋Š” ๊ฐ„๊ฒฐํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
4.
๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ : React๋Š” ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ์ง€์›ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์ด ๋‹จ์ผ ๋ฐฉํ–ฅ์œผ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ๊ทธ ๋ฐ˜๋Œ€๋Š” ์ž๋™์œผ๋กœ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
5.
์ƒํƒœ ๊ด€๋ฆฌ : React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋ฅผ ํ†ตํ•ด ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒํƒœ๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒํƒœ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ํ™”๋ฉด์ด ๊ฐฑ์‹ ๋ฉ๋‹ˆ๋‹ค.
6.
๋ผ์šฐํŒ… : React๋Š” ๋ผ์šฐํ„ฐ๋ฅผ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ํŽ˜์ด์ง€ ๊ฐ„์˜ ์ „ํ™˜์ด๋‚˜ SPA์—์„œ์˜ ๋ผ์šฐํŒ…์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. React Router๋Š” React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ผ์šฐํŒ…์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

์ฃผ์š” ํ•™์Šต ์š”์†Œ

1.
JSX
2.
์ปดํฌ๋„ŒํŠธ
3.
Props
4.
State
5.
๋ผ์ดํ”„์‚ฌ์ดํด(์ƒ๋ช…์ฃผ๊ธฐ)
6.
Hooks
7.
์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง
8.
์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง

JSX (JavaScript XML)

โ€ข
JSX๋Š” JavaScript์˜ ํ™•์žฅ ๋ฌธ๋ฒ•์œผ๋กœ, JavaScript ์ฝ”๋“œ ์•ˆ์—์„œ XML ํ˜•์‹์˜ ๋งˆํฌ์—…์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
React์—์„œ ์ปดํฌ๋„ŒํŠธ์˜ UI๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ๊ฐ€๋…์„ฑ์ด ๋†’๊ณ  ์ฝ”๋“œ ์ž‘์„ฑ์ด ํŽธ๋ฆฌํ•˜๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
const element = <h1>Hello, World!</h1>;
JavaScript
๋ณต์‚ฌ

์ปดํฌ๋„ŒํŠธ

โ€ข
์ปดํฌ๋„ŒํŠธ๋Š” UI๋ฅผ ๋…๋ฆฝ์ ์ด๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.
โ€ข
React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ์กฐํ•ฉ๋˜์–ด ์ „์ฒด UI๋ฅผ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค.
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
JavaScript
๋ณต์‚ฌ

Props (์†์„ฑ)

โ€ข
Props๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.
โ€ข
์ปดํฌ๋„ŒํŠธ๋Š” props๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<Welcome name="John" />
JavaScript
๋ณต์‚ฌ

State (์ƒํƒœ)

โ€ข
State๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์ฒด์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.
โ€ข
์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด React๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌ๋ Œ๋”๋งํ•˜๊ณ  ๋ณ€๊ฒฝ๋œ ์ƒํƒœ๋ฅผ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.
class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } // ... }
JavaScript
๋ณต์‚ฌ

๋ผ์ดํ”„์‚ฌ์ดํด(์ƒ๋ช…์ฃผ๊ธฐ)

โ€ข
์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒ์„ฑ, ๊ฐฑ์‹ , ์ œ๊ฑฐ๋˜๋Š” ๊ณผ์ •์„ ๋ผ์ดํ”„์‚ฌ์ดํด์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
์ฃผ์š” ๋‹จ๊ณ„์— ๋”ฐ๋ผ ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
class ExampleComponent extends React.Component { componentDidMount() { // ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋œ ํ›„ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ } // ... }
JavaScript
๋ณต์‚ฌ

Hooks

โ€ข
Hooks๋Š” ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ์™€ ์ƒ๋ช…์ฃผ๊ธฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
โ€ข
useState, useEffect ๋“ฑ์ด ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋ฉฐ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ์™€ ๋ผ์ดํ”„์‚ฌ์ดํด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
import React, { useState, useEffect } from 'react'; function ExampleComponent() { const [count, setCount] = useState(0); useEffect(() => { // ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ๋˜๋Š” ๊ฐฑ์‹ ๋œ ํ›„ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ }, [count]); // ... }
JavaScript
๋ณต์‚ฌ

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

โ€ข
React์—์„œ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง์ด ์ผ๋ฐ˜์ ์ธ HTML๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, camelCase ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
โ€ข
ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  JSX์—์„œ ์ด๋ฒคํŠธ์— ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
function handleClick() { alert('Button Clicked!'); } <button onClick={handleClick}>Click me</button>
JavaScript
๋ณต์‚ฌ

์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง

โ€ข
JSX์—์„œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋‚˜ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋‚˜ && ์—ฐ์‚ฐ์ž ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
function Greeting({ isLoggedIn }) { return isLoggedIn ? <UserGreeting /> : <GuestGreeting />; }
JavaScript
๋ณต์‚ฌ

React & React Native

๋‘ ๊ธฐ์ˆ  ๋ชจ๋‘ Facebook์—์„œ ๊ฐœ๋ฐœ๋˜์—ˆ์œผ๋ฉฐ, React์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ์ˆ ๊ณผ ๊ฐœ๋…์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. React Native๋Š” React๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ JavaScript ๋ฐ React๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„ค์ดํ‹ฐ๋ธŒ ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์š” ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์œ ์‚ฌ์„ฑ์œผ๋กœ ์ธํ•ด React๋กœ ์ž‘์„ฑ๋œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ React Native๋กœ ์ž‘์„ฑ๋œ ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ„์˜ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์ด ์ผ๋ถ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

React

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
โ€ข
์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ: React๋Š” ์ฃผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
โ€ข
์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜: React๋Š” ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค. ์ด๋Š” UI๋ฅผ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ  ๋…๋ฆฝ์ ์ธ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ฐœ๋ฐœํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค€๋‹ค.
React๋กœ ๊ฐœ๋ฐœํ•œ ์›น์‚ฌ์ดํŠธ : Facebook, Instagram

React Native

๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
โ€ข
๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ: React Native๋Š” React์˜ ํ™•์žฅ์œผ๋กœ, ์ฃผ๋กœ iOS ๋ฐ Android ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
โ€ข
๋„ค์ดํ‹ฐ๋ธŒ ๋ชจ๋ฐ”์ผ ์•ฑ ๊ฐœ๋ฐœ: React Native๋Š” JavaScript์™€ React๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„ค์ดํ‹ฐ๋ธŒ(์›๋ž˜์˜ ํ”Œ๋žซํผ์—์„œ ์‹คํ–‰๋˜๋Š”) ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜์˜ ํ™•์žฅ: React Native๋„ React์ฒ˜๋Ÿผ ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, React์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌํ•œ ๊ฐœ๋…๊ณผ API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
React Native ๋กœ ๊ฐœ๋ฐœํ•œ ์•ฑ : Facebook, Instagram

React ์‚ฌ์šฉ ์ „ vs ์‚ฌ์šฉ ํ›„

React ์‚ฌ์šฉ ํ•˜์ง€ ์•Š๋Š” HTML ์ฝ”๋“œ

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Styled Alert Example</title> <style> /* ์Šคํƒ€์ผ ์ถ”๊ฐ€ */ .styled-alert { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; background-color: #f1f1f1; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } </style> </head> <body> <button id="myButton">Click me</button> <script> // ๋ฒ„ํŠผ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ var myButton = document.getElementById("myButton"); // ๋ฒ„ํŠผ์— ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ถ”๊ฐ€ myButton.addEventListener("click", function() { // ์Šคํƒ€์ผ๋ง๋œ alert๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•œ div ์š”์†Œ ์ƒ์„ฑ var styledAlert = document.createElement("div"); styledAlert.className = "styled-alert"; styledAlert.textContent = "๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!"; // body์— ์ถ”๊ฐ€ document.body.appendChild(styledAlert); // 2์ดˆ ํ›„์— alert๋ฅผ ์‚ญ์ œ setTimeout(function() { document.body.removeChild(styledAlert); }, 2000); }); </script> </body> </html>
JavaScript
๋ณต์‚ฌ

React ๋กœ ๊ฐœ๋ฐœํ•œ ์ฝ”๋“œ

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>React Styled Alert</title> <!-- React ๋ฐ ReactDOM CDN ์ถ”๊ฐ€ --> <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <!-- Babel ์‚ฌ์šฉ์„ ์œ„ํ•œ CDN ์ถ”๊ฐ€ --> <script crossorigin src="https://unpkg.com/@babel/standalone/babel.min.js"></script> <style> /* ์Šคํƒ€์ผ ์ถ”๊ฐ€ */ .styled-alert { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; background-color: #f1f1f1; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } </style> </head> <body> <div id="root"></div> <script type="text/babel"> // React ์ปดํฌ๋„ŒํŠธ ์ •์˜ const { useState } = React; function App() { // showAlert ์ƒํƒœ์™€ ํ•ด๋‹น ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ useState ํ›…์„ ํ†ตํ•ด ์ƒ์„ฑ const [showAlert, setShowAlert] = useState(false); // handleClick ํ•จ์ˆ˜ ์ •์˜: ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ showAlert ์ƒํƒœ๋ฅผ ํ† ๊ธ€ํ•˜๊ณ , 2์ดˆ ํ›„์— ๋‹ค์‹œ false๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ alert๋ฅผ ์ œ๊ฑฐ const handleClick = () => { // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ showAlert ์ƒํƒœ๋ฅผ ํ† ๊ธ€ setShowAlert(true); // 2์ดˆ ํ›„์— showAlert ์ƒํƒœ๋ฅผ ๋‹ค์‹œ false๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ alert๋ฅผ ์ œ๊ฑฐ setTimeout(() => { setShowAlert(false); }, 2000); }; // JSX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ UI๋ฅผ ๋ฐ˜ํ™˜ return ( <div> {/* ๋ฒ„ํŠผ ์—˜๋ฆฌ๋จผํŠธ: ํด๋ฆญ ์‹œ handleClick ํ•จ์ˆ˜ ์‹คํ–‰ */} <button onClick={handleClick}>Click me</button> {/* showAlert๊ฐ€ true์ผ ๋•Œ๋งŒ ๋ Œ๋”๋ง๋˜๋Š” ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง */} {showAlert && ( <div className="styled-alert"> ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! </div> )} </div> ); } // React ์ปดํฌ๋„ŒํŠธ๋ฅผ root ์š”์†Œ์— ๋ Œ๋”๋ง ReactDOM.render(<App />, document.getElementById('root')); </script> </body> </html>
JavaScript
๋ณต์‚ฌ

๋ฏธ๋ฆฌ ์•Œ์•„๋‘๊ธฐ

React๋ฅผ ํ”„๋กœ์ ํŠธ์— ํฌํ•จํ•˜๋Š” ๋ฐฉ์‹

1.
CDN ๋ฐฉ์‹
2.
Node.js์™€ npm ๋ฐฉ์‹

CDN ๋ฐฉ์‹

1.
React ๋ฐ ReactDOM CDN ์ถ”๊ฐ€
2.
Babel์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
3.
React ์ฝ”๋“œ ์ž‘์„ฑ
React ๋ฐ ReactDOM CDN ์ถ”๊ฐ€
<!-- React์™€ ReactDOM CDN ์ถ”๊ฐ€ --> <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
HTML
๋ณต์‚ฌ
Babel์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
<!-- Babel์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ Babel CDN ์ถ”๊ฐ€ --> <script crossorigin src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
HTML
๋ณต์‚ฌ
React ์ฝ”๋“œ ์ž‘์„ฑ
<script type="text/babel"> // React ์ฝ”๋“œ ์ž‘์„ฑ const element = <h1>Hello, World!</h1>; ReactDOM.render(element, document.getElementById('root')); </script>
HTML
๋ณต์‚ฌ
๋ฐ”๋ฒจ()์ด๋ž€? ๋ฐ”๋ฒจ(Babel)์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๋„๊ตฌ๋กœ, ์ฃผ๋กœ ์ตœ์‹  ECMAScript ํ‘œ์ค€์— ๋”ฐ๋ผ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ์ด์ „ ๋ฒ„์ „์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

Node.js์™€ npm ๋ฐฉ์‹

1.
ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™”
2.
React ๋ฐ ReactDOM ์„ค์น˜
3.
React ์ฝ”๋“œ ์ž‘์„ฑ
ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™”
npx create-react-app my-react-app cd my-react-app
Shell
๋ณต์‚ฌ
React ๋ฐ ReactDOM ์„ค์น˜
npm install react react-dom
Shell
๋ณต์‚ฌ
React ์ฝ”๋“œ ์ž‘์„ฑ
import React from 'react'; import ReactDOM from 'react-dom'; const element = <h1>Hello, World!</h1>; ReactDOM.render(element, document.getElementById('root'));
JavaScript
๋ณต์‚ฌ