๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

ยท ์•ฝ 3๋ถ„

๊ณต๋ถ€๋Š” ์–ด๋–ป๊ฒŒ?#

๊ตฌ๊ธ€๋ง#

  • ์˜์–ด vs ํ•œ๊ตญ์–ด

    ํ•œ๊ตญ์–ด๋Š” ์ปจํ…์ธ ๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค. ๊ณต์‹ ์‚ฌ์ดํŠธ๋ฅผ ๋ณด์ž.

MDN#

stackoverflow#

  • ์งˆ๋ฌธ์„ ๋‚จ๊ธฐ๊ณ  ๋‹ต๋ณ€์„ ๋‹ฌ์•„๋ผ

๋””๋ฒ„๊น…#

  • ์ธ์ƒ์˜ ์ ˆ๋ฐ˜ ์ด์ƒ์„ ๋””๋ฒ„๊น…์— ์Ÿ๋Š”๋‹ค

  • ๋””๋ฒ„๊น…ํ•˜๋ฉด์„œ ํ•™์Šตํ•˜๋ผ

  • this , closure ๋“ฑ๋“ฑ ์–ด๋ ค์šด ๊ฐœ๋…์€ ๋””๋ฒ„๊น… ์ฐฝ์„ ๋„์šฐ๊ณ  ์ฝœ ์Šคํƒ์— ์Œ“์ธ ์ง€์—ญ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ญ”์ง€ ๋ณด๋ฉด์„œ ํ•™์Šต

  • ๋””๋ฒ„๊น… ์ฃผ๋„ ํ•™์Šต

  • ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๋ฌด์‹œํ•˜์ง€ ๋งˆ๋ผ

    ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ž์ฃผ ๋ด์•ผ์ง€ ๋‹ค์Œ ๋ฒˆ์— ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋งŒ ๋ณด๊ณ  ์ƒํ™ฉ์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Œ

JS์˜ ํ‘œ์ค€#

  • ECMAScript

  • ES 2015 == ES6

  • prototype ๋ชฐ๋ผ๋„ ๊ฐœ๋ฐœํ•  ์ˆœ ์žˆ์ง€๋งŒ, JS์˜ ํ•ต์‹ฌ์ด prototype ์ด๋‹ค. ๊ผญ ์ดํ•ดํ•ด์•ผํ•œ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ#

ํ˜„์‹ค ์„ธ๊ณ„์— ์žˆ๋Š” ์‚ฌ๋ฌผ. ์‹ค์กดํ•˜๋Š” ๊ฒƒ.

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ์€ ์—ญํ• 

์—ญํ• ์„ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

new ํ‚ค์›Œ๋“œ ์ƒ์„ฑ์ž, ์†์„ฑ์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

์Šคํƒ€๋ฒ…์Šค ์ปคํ”ผ, ๋ฐ”๋ฆฌ์Šคํƒ€์˜ ์˜ˆ์‹œ

์˜์กด์„ฑ์ด ๋‚ฎ์€ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์—ฐ๊ฒฐ์„ ๋Š์Šจํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. โ†’ ๋””์ž์ธ ํŒจํ„ด

์˜์กด์„ฑ์ด ๋‚ฎ์•„์„œ ์ˆ˜์ •์ด ๋˜๋„ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Œ

์ฝ”๋”ฉ๋ณด๋‹ค ๋””์ž์ธ

๋ฉ”์†Œ๋“œ vs ํ”„๋กœํ† ํƒ€์ž…#


๋ฉ”์†Œ๋“œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐจ์ง€

ํ”„๋กœํ† ํƒ€์ž…์€ ๊ฐ ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์‰์–ด ํ•จ.

module vs import#

์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜์™€ private#

๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ#

JavaScript๋Š” ์‹ฑ๊ธ€์Šค๋ ˆ๋“œ๋กœ ๋™์ž‘

then์ด ์‹คํ–‰๋œ ๊ฒŒ ์•„๋‹ˆ๋ผ then์— ๋“ฑ๋ก๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

  • ํด๋กœ์ €

  • ์ด๋ฒคํŠธ ๋ฃจํ”„


ยท ์•ฝ 6๋ถ„

๋‚˜์˜ ๊ฐ์˜ค/๊ณ„ํš#

  1. ์šฐ์•„ํ•œํ…Œํฌ์บ ํ”„ ๋๋‚˜๊ณ  ํ•จ๊ป˜ ํ”„๋กœ์ ํŠธํ•  ์‚ฌ๋žŒ๋“ค ๋งŒ๋‚˜๊ธฐ

  2. ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์˜๊ฒฌ ์ž˜ ๋“ฃ๊ธฐ

  3. ๋‚ด๊ฐ€ ์•„๋Š” ๊ฒƒ์€ ์นœ์ ˆํžˆ ์•Œ๋ ค์ฃผ๊ธฐ

  4. ๋ชจ๋ฅด๋Š” ๊ฒƒ์€ ์ž˜ ์งˆ๋ฌธํ•˜๊ธฐ

  5. ๋ฐฐ์šด ๊ฒƒ์€ ๊ฐœ์ธ์ ์œผ๋กœ ์จ๋จน์–ด ๋ณด๊ธฐ

  6. ์˜ค๋Š˜ ๋ฐฐ์šด ๋‚ด์šฉ์€ ์˜ค๋Š˜ ๋ณต์Šตํ•˜๊ธฐ

Git / Github#

Git์— ๊ด€ํ•˜์—ฌ#

Git์€ ๋ฆฌ๋ˆ„์Šค ํ† ๋ฐœ์ฆˆ(Linus Torvalds)๊ฐ€ ๋ฆฌ๋ˆ…์Šค(Linux) ์ปค๋„์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งŒ๋“ ์†Œ์Šค๊ด€๋ฆฌ ํˆด์ด๋‹ค. (ใ„ทใ„ท) Git ์ด์ „์˜ ํ˜•์ƒ๊ด€๋ฆฌ ํˆด์€ SVN, CVS ๋“ฑ์ด ์žˆ์—ˆ์Œ.

๊ฐ„๋‹จํ•œ ์†Œ๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ์— Git์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋‚œ์ด๋„๋ฅผ ๋†’์ด์ง€๋งŒ, ๋ณต์žกํ•œ ๋Œ€๊ทœ๋ชจํ”„๋กœ์ ํŠธ์—์„œ๋Š” ํ”„๋กœ์ ํŠธ์˜ ๋‚œ์ด๋„๋ฅผ ์ƒ๋‹นํžˆ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์ˆ˜์ ์ด๋‹ค.

์•ˆํ‹ฐ ํŒจํ„ด#

  • git add . : ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ๋Š” ๋ชจ๋“  ํŒŒ์ผ์„ ํ•œ๋ฒˆ์— ์Šคํ…Œ์ด์ง• ํ•˜์ง€ ๋ง์•„๋ผ. ํ•œ ํŒŒ์ผํ•œ ํŒŒ์ผ ์”ฉ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ํ™•์ธํ•˜๋ฉด์„œ ์ปค๋ฐ‹ํ•ด๋ผ.

  • git commit -m "" : ์ปค๋ฐ‹์€ git commit ์œผ๋กœ ์ œ๋ชฉ๊ณผ ํ•จ๊ป˜ ๊ฐ„๋‹จํ•œ ์„ค๋ช…๊นŒ์ง€ ๋ง๋ถ™์—ฌ์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ •์„์ด๋‹ค.

์ด๋Ÿฌํ•œ ์•ˆํ‹ฐ ํŒจํ„ด์ด ๋งŒ์—ฐํ•ด ์žˆ์Œ. ์ข‹์€ ์ปค๋ฐ‹ ์Šต๊ด€์„ ๊ธฐ๋ฅด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

Git flow#

Git-flow, Github flow, Gitlab flow ๊ฐ๊ฐ์ด ๋‹ค๋ฅธ ํ˜•์ƒ ๊ด€๋ฆฌ ์ „๋žต์ด๋‹ค. ์ด ์™ธ์—๋„ ์ˆ˜๋งŽ์€ flow ๋“ค์ด ์กด์žฌ. ํ•˜์ง€๋งŒ Git-flow ๋ผ๋Š” ์ด๋ฆ„ ๋•Œ๋ฌธ์— Git flow๊ฐ€ ์ •์„, ํ‘œ์ค€์ด๋ผ๋Š” ์˜คํ•ด๋ฅผ ํ•˜๊ธฐ ์‰ฝ๋‹ค. ํ•˜์ง€๋งŒ Git flow๊ฐ€ ๋ณต์žกํ•˜๊ณ  ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์€ ์กฐ์ง, ํŒ€์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. Git flow ์™ธ์—๋„ ํ›Œ๋ฅญํ•œ flow ์ „๋žต๋“ค์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— Git flow์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€๋งŒ ๋ฌด์กฐ๊ฑด GIt flow๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ดํ‘œ์ค€์ธ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

Understanding the GitHub flow

์šฐ๋ฆฐ Git-flow๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์š” - ์šฐ์•„ํ•œํ˜•์ œ๋“ค ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ

ํ•˜๋ฃจ์— 1000๋ฒˆ ๋ฐฐํฌํ•˜๋Š” ์กฐ์ง ๋˜๊ธฐ | ๋ฑ…ํฌ์ƒ๋Ÿฌ๋“œ

์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•ด#

  • ๋ฉ”์‹œ์ง€๋Š” ํ•œ๊ธ€๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

    ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋Š” ํŒ€์› ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฏธ๋ž˜์˜ ๋‚˜๋ฅผ ์œ„ํ•ด ๋‚จ๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค. ์ ๋‹นํ•œ ๋‹จ์–ด๋ฅผ์ฐพ์ง€ ๋ชปํ•œ ์–ด์ƒ‰ํ•œ ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ๋ณด๋‹ค ํ•œ๊ธ€๋กœ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ๋‚ซ๋‹ค.

    ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ๋Š” ์˜คํ”ˆ ์†Œ์Šค๊ฐ€ ์•„๋‹ˆ๋‹ˆ๊นŒ

    ํ•˜์ด๋ธŒ๋ฆฌ๋“œ๋กœ ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€ ์ œ๋ชฉ์„ ์˜์–ด๋กœ ๋‚จ๊ธฐ๊ณ  ์„ค๋ช…์„ ํ•œ๊ธ€๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

    ์‹ค์ œ๋กœ TDD๋ฅผ ์ž˜ํ•˜๋Š” ํŒ€์—์„  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ๋ฉ”์†Œ๋“œ ๋ช…์„ ํ•œ๊ธ€๋กœ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ํ›จ์”ฌ ๋ช…ํ™•ํ•œ ๋ฉ”์†Œ๋“œ ๋ช…์œผ๋กœ ์ธํ•ด ์ข‹์€ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค.

  • fork ํ›„ PR๊ณผ branch ํ›„ PR์˜ ์ฐจ์ด

    ๊ธฐ๋ณธ์ ์œผ๋กœ fork๋Š” ๋‚ด๊ฐ€ ์ด ์ €์žฅ์†Œ์— ๊ถŒํ•œ์ด ์—†์„ ๋•Œ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜คํ”ˆ ์†Œ์Šค์— ๊ธฐ์—ฌํ•˜๋Š” ๊ฒฝ์šฐ ๋“ฑ๋“ฑ..

Git ๋ช…๋ น์–ด์™€ ๋™์ž‘ ์›๋ฆฌ#

Git ๋™์ž‘ ์›๋ฆฌ๋ฅผ ์ œ๋Œ€๋กœ ๋ฐฐ์šด ๊ฒƒ์€ ์ฒ˜์Œ์ด์—ˆ๊ณ  ๊ธฐ์กด ์•Œ๊ณ  ์žˆ๋˜ ๊ฐœ๋…์ด ์ž˜๋ชป๋˜์—ˆ๋‹ค๋Š”๊ฒƒ์„ ๊นจ๋‹ซ๊ณ  ๊ฐœ์ธ์ ์œผ๋กœ ์ถฉ๊ฒฉ์„ ๋ฐ›์•˜๋‹ค.

Learn Git Branching

  • git init : ์ˆจ๊ฒจ์ง„ ํด๋”๊ฐ€ ํ•˜๋‚˜ ์ƒ๊น€

    • .git : ๋กœ์ปฌ ์ €์žฅ์†Œ

      • git์˜ ์ €์žฅ ๋‹จ์œ„๋Š” ์ปค๋ฐ‹

        • Working directory โ†’ Stage โ†’ Commit
    • GitHub or GitLab ... - ์›๊ฒฉ ์ €์žฅ์†Œ

    • git reset โ†’ ๋‚ ์•„๊ฐ€์ง€ ์•Š์Œ.

    • ์›Œํ‚น ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์Šค๋ƒ…์ƒท

    • ์ปค๋ฐ‹์€ ๋‚ด๋ถ€์ ์œผ๋กœ ํŠธ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ ํŠธ๋ฆฌ์— ์˜ค๋ธŒ์ ํŠธ๋“ค์ด ๋‹ฌ๋ ค์„œ ํ•˜๋‚˜์˜ ์ปค๋ฐ‹์„ ๊ตฌ์„ฑํ•จ

    • git cat-file -t ce013

    • git cat-file blob ce013

    • git cat-file -t HEAD

    • git log -n1

    • git cat-file -t 7da33cf4

    • git cat-file commit 7da33cf4

    • git ls-tree aaa96ced2d9a1c8e ...

    • branch๋Š” ํ—ค๋“œ์— ๋Œ€ํ•œ ์ฐธ์กฐ์ผ ๋ฟ

    • ์ปค๋ฐ‹์€ ๋ถ€๋ชจ ์ปค๋ฐ‹์ด ์žˆ๋‹ค

    • git checkout C4 vs git checkout hello

    • git์€ diff๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ „์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.

    • svn์€ diff๋ฅผ ์ €์žฅํ•œ๋‹ค

    • tree .git

    • ๋ธŒ๋žœ์น˜๋ฅผ ๋งŽ์ด ๋งŒ๋“ค์–ด๋„ ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

    • git status

    • git checkout master

    • ๋ธŒ๋žœ์น˜๋Š” ์ปค๋ฐ‹์— ๋Œ€ํ•œ ์ฐธ์กฐ์ผ ๋ฟ ์•„๋ฌด๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค.

    • ๊นƒ์—๋Š” ์ปค๋ฐ‹๋ฐ–์— ์—†๋‹ค. ํƒœ๊ทธ, ๋ธŒ๋žœ์น˜, ์–ด์ฉŒ๊ณ ๋Š” ๋‹ค ์ฐธ์กฐ ๋ณ€์ˆ˜์ผ ๋ฟ์ด๋‹ค ์‹ค์ œ ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋Š” ์•„๋ฌด๊ฒƒ๋„ ์—†๋‹ค.

    • git reset โ€”hard Head~1 ์‹œ์— ๋ณต์›ํ•˜๋Š” ๋ฒ•

    • git branch test ์ฒดํฌ์„ฌ

    • git reflog ๋ชจ๋“  ์ฐธ์กฐ ๋ณ€์ˆ˜๋“ค์˜ ๋กœ๊ทธ๋ฅผ ๋ณด๋Š” ๋ช…๋ น

    • git push ๋กœ์ปฌ ์ €์žฅ์†Œ์— ์žˆ๋Š” ๊ฒƒ์„ ์›๊ฒฉ ์ €์žฅ์†Œ๋กœ ๋ณด๋ƒ„

ยท ์•ฝ 2๋ถ„

Ajax#

Ajax(Asynchronous JavaScript and XML)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์„œ๋ฒ„์™€ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ฐฉ์‹์„์˜๋ฏธ


JSON#

JSON

JSON(JavaScript Object Notation)

JavaScript์—์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‘œํ˜„์‹์„ ์˜๋ฏธ

์ด ํ‘œํ˜„์‹์€ ์‚ฌ๋žŒ๋„ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ  ๊ธฐ๊ณ„๋„ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉด์„œ ๋ฐ์ดํ„ฐ์˜ ์šฉ๋Ÿ‰์ด ์ž‘๋‹ค. ์ด๋Ÿฐ ์ด์œ ๋กœ ์ตœ๊ทผ์—๋Š” JSON์ด XML์„ ๋Œ€์ฒดํ•ด์„œ ์„ค์ •์˜ ์ €์žฅ์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก๋“ฑ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.


{    "coord": {"lon": 139, "lat": 35},    "weather": [        {            "id": 800,            "main": "Clear",            "description": "clear sky",            "icon": "01n"        }    ],    "base": "stations",    "main": {        "temp": 281.52,        "feels_like": 278.99,        "temp_min": 280.15,        "temp_max": 283.71,        "pressure": 1016,        "humidity": 93    },    "wind": {        "speed": 0.47,        "deg": 107.538    },    "clouds": {        "all": 2    },    "dt": 1560350192,    "sys": {        "type": 3,        "id": 2019346,        "message": 0.0065,        "country": "JP",        "sunrise": 1560281377,        "sunset": 1560333478    },    "timezone": 32400,    "id": 1851632,    "name": "Shuzenji",    "cod": 200}

JSON API#

JSON.parse()#

์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

JSON.stringify()#

์ธ์ž๋กœ ์ „๋‹ฌ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.


ยท ์•ฝ 12๋ถ„

NPM#

NPM1 - YouTube

Node Package Manager : ์•ฑ์Šคํ† ์–ด์—์„œ ์•ฑ์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ NPM์—์„œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ• ์ˆ˜ ์žˆ์Œ.

ํŒจํ‚ค์ง€๋Š” -g (global)์— ์„ค์น˜ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋ถ€ํ’ˆ์œผ๋กœ์จ ์‚ฌ์šฉํ•˜๋„๋ก์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.


๊ฒ€์ƒ‰๊ณผ ์„ค์น˜#

npm

NPM์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ


๋งŒ์•ฝ local-web-server ๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์‹ถ์„ ๋•Œ,

  • npm i local-web-server

  • npm install local-web-server

i๋Š” install์˜ ์•ฝ์ž


์ปดํ“จํ„ฐ์˜ ์ „์—ญ์ ์ธ, ๋…๋ฆฝ์ ์ธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ์„ค์น˜ํ•˜๊ณ  ์‹ถ์„ ๋•Œ,

  • npm install -g local-web-server

g๋Š” global์˜ ์•ฝ์ž

๊ถŒํ•œ์ด ์—†๋‹ค๋ฉด sudo ๋ฅผ ์•ž์— ์ถ”๊ฐ€ํ•œ๋‹ค.


ํŒจํ‚ค์ง€ ๋ชฉ๋ก ๋ณด๊ธฐ์™€ ์—…๋ฐ์ดํŠธ, ์‚ญ์ œ#

ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์— ์„ค์น˜๋œ ํŒจํ‚ค์ง€ ๋ณด๊ธฐ

npm list

์ „์—ญ์— ์„ค์น˜๋œ ํŒจํ‚ค์ง€ ๋ณด๊ธฐ

npm list -g

์ „์—ญ์— ์„ค์น˜๋œ ํŒจํ‚ค์ง€ ์ค‘ ์ œ์ผ ์ƒ๋‹จ์— ์žˆ๋Š” ํŒจํ‚ค์ง€๋งŒ ๋ณด๊ธฐ

npm list -g --depth=0

์ „์—ญ์— ์žˆ๋Š” ํŒจํ‚ค์ง€ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ

npm update -g local-web-server

์ „์—ญ์— ์žˆ๋Š” ํŒจํ‚ค์ง€ ์‚ญ์ œํ•˜๊ธฐ

npm uninstall -g local-web-server


๐Ÿ“Œ ๋ฐ”ํ€ด๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ค์ง€ ๋งˆ๋ผ!


React#

React - YouTube

React ์•ฑ ๋งŒ๋“ค๊ธฐ#

  • npm install -g create-react-app - ์ „์—ญ์— ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.
npm install -g create-react-appmkdir my-appcd my-appcreate-react-app .

  • npx - ์ž„์‹œ๋กœ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์ง€์šฐ๋Š” ์‹คํ–‰ ๋„๊ตฌ.
npx create-react-app my-appcd my-appnpm start

React ์•ฑ ์‹คํ–‰#

npm run start


์ฝ”๋”ฉํ•˜๊ธฐ#

public ๋””๋ ‰ํ† ๋ฆฌ - static ์ž์›์ด ์œ„์น˜ํ•œ ๊ณณ.#

  • index.html
<div id="root"></div>

์ด root ์•ˆ์— ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.


src ๋””๋ ‰ํ† ๋ฆฌ - source ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ๊ณณ.#

  • index.js
ReactDOM.render(<App />, document.getElementById('root'));

<App /> ์ปดํฌ๋„ŒํŠธ๋ฅผ root ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ „๋‹ฌํ•˜๊ฒŒ๋œ๋‹ค.


  • App.js ํด๋ž˜์Šคํ˜• (ํด๋ž˜์Šคํ˜•๊ณผ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š”๋ฐ ์ด ๊ฐ•์ขŒ๋Š” ํด๋ž˜์Šคํ˜•์œผ๋กœ์ง„ํ–‰)
import React from 'react';import logo from './logo.svg';import './App.css';
class App extends React.Component {    render() {        return (            <div className="App">                <header className="App-header">                    <img src={logo} className="App-logo" alt="logo" />                    <p>                        Edit <code>src/App.js</code> and save to reload.                    </p>                    <a                        className="App-link"                        href="https://reactjs.org"                        target="_blank"                        rel="noopener noreferrer"                    >                        Learn React                    </a>                </header>            </div>        );    }}
export default App;

โ˜๏ธ ๋ฐ˜๋“œ์‹œ ๋ฆฌ์•กํŠธ๋Š” ๋ฐ˜ํ™˜ํ•  ๋•Œ ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค.


๋ฐฐํฌํ•˜๊ธฐ#

  • npm run build - ๋ฆฌ์•กํŠธ ์•ฑ์„ ๋นŒ๋“œํ•ด์คŒ

  • npx serve -s build - ๋นŒ๋“œํ•œ ์•ฑ์œผ๋กœ ๋กœ์ปฌ ํ˜ธ์ŠคํŒ…


JSX#

๐Ÿ“Œ JSX๋Š” JavaScript๊ฐ€ ์•„๋‹ˆ๋‹ค!

JSX๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” create-react-app์— ์˜ํ•ด JavaScript ์ฝ”๋“œ๋กœ ์ปจ๋ฒ„ํŒ… ๋œ๋‹ค.


Hello World!#

import React from 'react';import './App.css';
class App extends React.Component {    render() {        return <div className="App">Hello World!</div>;    }}
export default App;

App class๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™•์žฅํ•˜๊ณ  render ๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค. (ํ•„์ˆ˜), ํด๋ž˜์Šค ๋‚ด๋ถ€ ๋ฉ”์†Œ๋“œ์—์„œ function ํ‚ค์›Œ๋“œ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. (ES6)


Components ์™€ props#

์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•  ๋•Œ ์™„์ „ํžˆ ๋˜‘๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ํ•œ๊ณ„๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํŠน์ • ์†์„ฑ์„ ๊ฐ€์ง์— ๋”ฐ๋ผ ๋ณ€๊ฒฝํ•ด์ค„ ์ˆ˜ ์žˆ๋„๋ก props ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import React from 'react';import './App.css';
class Subject extends React.Component {    render() {        return (            <header>                <h1>{this.props.title}</h1>                {this.props.sub}            </header>        );    }}
class App extends React.Component {    render() {        return (            <div className="App">                <Subject title="WEB" sub="World Wide Web!" />                <Subject title="React" sub="For UI!" />            </div>        );    }}
export default App;

React Developer Tools#

React Developer Tools


์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ#

App.js

import React from 'react';import Subject from './components/Subject';import Content from './components/Content';import TOC from './components/TOC';import './App.css';
class App extends React.Component {    render() {        return (            <div className="App">                <Subject title="WEB" sub="World Wide Web!" />                <Subject title="React" sub="For UI!" />                <Content text="HTML is FUN!" />                <TOC />            </div>        );    }}
export default App;

import React from 'react'; - ๋ฆฌ์•กํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์ˆ˜.

ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋Š” import ์— ๋ช…์‹œ.

ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก export ์— ๋ช…์‹œ.


props ์™€ state#

2020-03-17-200317-image-0

props ๋Š” ์™ธ๋ถ€์—์„œ, ๋‚ด๋ถ€๋กœ ์ „๋‹ฌ. ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ์— ์†์„ฑ์— ๊ธฐ์ž…ํ•˜๋ฉด ๋‚ด๋ถ€ ์ปดํฌ๋„ŒํŠธ์—์„œ props ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ. ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ ์กฐ์ž‘.

state ๋Š” ๋‚ด๋ถ€์˜ ์ƒํƒœ ๊ฐ’.


App.js

import React from 'react';import Subject from './components/Subject';import TOC from './components/TOC';import Content from './components/Content';import './App.css';
class App extends React.Component {    constructor(props) {        super(props);        this.state = {            subject: {title: 'WEB', sub: 'World Wide Web!'},        };    }    render() {        return (            <div className="App">                <Subject                    title={this.state.subject.title}                    sub={this.state.subject.sub}                />                <Subject title="React" sub="For UI!" />                <TOC />                <Content text="HTML is FUN!" />            </div>        );    }}
export default App;

constructor : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜ ( state ๊ฐ’์„ ์ดˆ๊ธฐํ™”)

ํ•ด๋‹น ๊ฐ์ฒด ์•ˆ์—์„œ { this.state.subject.title } ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ.


TOC ๊ฐœ์„ #

TOC.js

import React from 'react';
class TOC extends React.Component {    render() {        var lists = [];        var data = this.props.data;        var i = 0;
        while (i < data.length) {            lists.push(                <li key={data[i].id}>                    <a href={'/content/' + data[i].id}>{data[i].title}</a>                </li>,            );            i = i + 1;        }
        return (            <nav>                <ul>{lists}</ul>            </nav>        );    }}
export default TOC;

TOC.js ์—์„œ ์™ธ๋ถ€์—์„œ data (๊ฐ์ฒด๊ฐ€ ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ)๋ฅผ props ๋กœ ์ „๋‹ฌ๋ฐ›๊ณ , ํ•ด๋‹น๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ lists ์— ๋‹ด์•„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฐ”๊พผ๋‹ค.

์ด ๋•Œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชฉ๋ก์€ key ๋ฅผ ๊ฐ€์ ธ์•ผํ•œ๋‹ค. key={data[i].id} ๊ฐ€ ํ•„์ˆ˜.

ํ‚ค๋ฅผ ๊ธฐ์ž…ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ.

2020-03-17-200317-image-1


App.js

import React from 'react';import Subject from './components/Subject';import TOC from './components/TOC';import Content from './components/Content';import './App.css';
class App extends React.Component {    constructor(props) {        super(props);        this.state = {            subject: {title: 'WEB', sub: 'World Wide Web!'},            contents: [                {id: 1, title: 'HTML', desc: 'HTML is for information'},                {id: 2, title: 'CSS', desc: 'CSS is for Design'},                {id: 3, title: 'JavaScript', desc: 'JavaScript is for Interactive'},            ],        };    }    render() {        return (            <div className="App">                <Subject                    title={this.state.subject.title}                    sub={this.state.subject.sub}                />                <TOC data={this.state.contents} />                <Content text="HTML is FUN!" />            </div>        );    }}
export default App;

์ด๋ ‡๊ฒŒ ์™ธ๋ถ€์—์„œ Contents ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.


์ด๋ฒคํŠธ state , props , render() ํ•จ์ˆ˜#

React ์—์„œ๋Š” props ๋˜๋Š” state ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ render() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก ์•ฝ์†๋˜์–ด ์žˆ๋‹ค. โ†’ ํ™”๋ฉด์ด ๋‹ค์‹œ ๊ทธ๋ ค์ง„๋‹ค.


๋™์ ์œผ๋กœ ๋ณ€์ˆ˜ ์ œ์–ด#

Subject.title ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ, this.state.mode ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ๋‹ค. ์ด ๋•Œ Subject.title ์€ <a> ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ๋‹ค.

onClick ์†์„ฑ์œผ๋กœ ํด๋ฆญํ–ˆ์„ ๋•Œ ์‹คํ–‰ํ•  ํ•จ์ˆ˜(์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ)๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ onClick ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ํ™”๋ฉด์ด ์ƒˆ๋กœ๊ณ ์นจ ๋œ๋‹ค. <a> ํƒœ๊ทธ์˜ ๊ธฐ๋ณธ ๋™์ž‘ ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ.


...
<h1><a href="/" onClick={function(e){  console.log(e);    // debugger;  e.preventDefault();}}>{this.state.subject.title}</a></h1>
...

โ˜๏ธ debugger; ๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น ์œ„์น˜์—์„œ ๋ฉˆ์ถฐ์ง€๊ณ  ๊ฐœ๋ฐœ์ž๋„๊ตฌ์—์„œ ํ•ด๋‹น์œ„์น˜์˜ ์†Œ์Šค๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ


์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ํƒœ๊ทธ์˜ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘๋ฐฉ๋ฒ•์„ ๋ชปํ•˜๊ฒŒ ๋ง‰๋Š” ๊ฒƒ์ด event ๊ฐ์ฒด์˜ preventDefault() ๋ผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ๊ณ ์นจ๋˜๋Š” ํ˜„์ƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.


state ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ.

return (    <div className="App">        <header>            <h1>                <a                    href="/"                    onClick={function (e) {                        e.preventDefault();                        this.setState({                            mode: 'welcome',                        });                    }.bind(this)}                >                    {this.state.subject.title}                </a>            </h1>            {this.state.subject.sub}        </header>        <TOC data={this.state.contents} />        <Content title={_title} desc={_desc} />    </div>);
  • ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋์„ ๋•Œ ์‹คํ–‰๋˜๋Š” function ๋‚ด๋ถ€์— this ์˜ ๊ฐ’์ด ์•„๋ฌด๊ฒƒ๋„ ์„ธํŒ…๋˜์–ด์žˆ์ง€ ์•Š๋‹ค. โ†’ this ๋ฐ”์ธ๋”ฉ ์‚ฌ์šฉํ•ด์•ผํ•จ. ํ•จ์ˆ˜ ์•ˆ์˜ this ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„ฃ์€ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•จ. ( this = App )

  • state ๋ฅผ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•  ๋• setState ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ


React์—์„œ ๊ฐ์ฒด ๋˜๋Š” ๋ฐฐ์—ด state ๋ณ€๊ฒฝ#

React์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ state ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ render ํ•จ์ˆ˜์™€ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์˜ render ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด reload ํ•˜๊ฒŒ ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—๋Š” ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์—†๋‹ค๋ฉด ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด React.Component ์˜ shouldComponentUpdate ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

shouldComponentUpdate ๋Š” newProps ์™€ newState ๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ์—ฌ๊ธฐ์„œ newProps ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ๋ฐ”๋€ props ๊ฐ’์„ ๋งํ•œ๋‹ค.

newProps ์™€ ์ด์ „ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” this.props.data ๋ฅผ ์ด์šฉํ•ด props ๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„๋•Œ๋งŒ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ render ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. (๋‘˜์ด ๊ฐ™์œผ๋ฉด false ๋ฆฌํ„ดํ•˜๊ณ  ๋‘˜์ด ๋‹ค๋ฅด๋ฉด true ๋ฅผ ๋ฆฌํ„ดํ•จ์œผ๋กœ ๊ฐ€๋Šฅ)

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ state ์— ๊ฐ์ฒด ๋˜๋Š” ๋ฐฐ์—ด์ด ์žˆ์„ ๊ฒฝ์šฐ์—, ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์˜ ์›๋ณธ์„ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด state ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ฒŒ ๋˜๋ฉด(์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐฐ์—ด์˜ push ํ•จ์ˆ˜๋Š” ์›๋ณธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.) ์›๋ณธ์ด ์ˆ˜์ •๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— newProps ์™€ this.props.data ๋Š” ๊ฐ™์„ ์ˆ˜๋ฐ–์— ์—†๊ณ , ๋”ฐ๋ผ์„œ shouldComponentUpdate ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ์กฐ์ž‘์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์ง€๊ณ  ์ด๋Š” ์„ฑ๋Šฅ ๊ฐœ์„ ์„ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฐฐ์—ด์—์„œ๋Š” push ํ•จ์ˆ˜ ๋Œ€์‹  concat , ๊ฐ์ฒด์—์„œ๋Š” Object.assign ํ•จ์ˆ˜ ๋“ฑ์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋กœ state ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์œผ๋กœ shouldComponentUpdate ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ์กฐ์ž‘์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ยท ์•ฝ 7๋ถ„

๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ๋ฆฌ์•กํŠธ

React ์ž…๋ฌธ#

Input ์ƒํƒœ ๊ด€๋ฆฌํ•˜๊ธฐ#

์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” input ํƒœ๊ทธ์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

  • input์— ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์˜ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋จ.

  • ์ดˆ๊ธฐํ™”๋ฅผ ํด๋ฆญํ•˜๋ฉด input ๋‚ด๋ถ€์— ๋‚ด์šฉ๋„ ์ง€์›Œ์ง€๊ณ , ์•„๋ž˜์˜ ๊ฐ’๋„ ์ง€์›Œ์ง.


InputSample.js

import React, {useState} from 'react';
function InputSample() {    const [text, setText] = useState('');
    const onChange = (e) => {        setText(e.target.value);    };
    const onReset = () => {        setText('');    };
    return (        <div>            <input onChange={onChange} value={text} />            <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>            <div>                <b>๊ฐ’: {text}</b>            </div>        </div>    );}
export default InputSample;

App.js

import React from 'react';import InputSample from './InputSample';
function App() {    return <InputSample />;}
export default App;

input ์˜ onChange ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉ. ์ด๋ฒคํŠธ ๊ฐ์ฒด e ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉ . e.target ์€ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ DOM์ธ input DOM์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋˜๊ณ , DOM์˜ value ๊ฐ’, e.target.value ๋ฅผ ์กฐํšŒํ•˜๋ฉด ํ˜„์žฌ input ์— ์ž…๋ ฅํ•œ ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค .


Event | PoiemaWeb


์—ฌ๋Ÿฌ ๊ฐœ์˜ input ์ƒํƒœ ๊ด€๋ฆฌํ•˜๊ธฐ#

input ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ ๋‹จ์ˆœํžˆ useState ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉํ•˜๊ณ  onChange ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์€ input ์— name ์„ ์„ค์ •ํ•˜๊ณ  ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ด ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ œ useState ์—์„œ๋Š” ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ํ˜•ํƒœ์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.


InputSample.js

import React, {useState} from 'react';
function InputSample() {    const [inputs, setInputs] = useState({        name: '',        nickname: '',    });
    const {name, nickname} = inputs; // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ํ†ตํ•ด ๊ฐ’ ์ถ”์ถœ
    const onChange = (e) => {        const {value, name} = e.target; // ์šฐ์„  e.target ์—์„œ name ๊ณผ value ๋ฅผ ์ถ”์ถœ        setInputs({            ...inputs, // ๊ธฐ์กด์˜ input ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋’ค            [name]: value, // name ํ‚ค๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ value ๋กœ ์„ค์ •        });    };
    const onReset = () => {        setInputs({            name: '',            nickname: '',        });    };
    return (        <div>            <input name="name" placeholder="์ด๋ฆ„" onChange={onChange} value={name} />            <input                name="nickname"                placeholder="๋‹‰๋„ค์ž„"                onChange={onChange}                value={nickname}            />            <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>            <div>                <b>๊ฐ’: </b>                {name} ({nickname})            </div>        </div>    );}
export default InputSample;

const onChange = (e) => {    const {value, name} = e.target; // ์šฐ์„  e.target ์—์„œ name ๊ณผ value ๋ฅผ ์ถ”์ถœ    setInputs({        ...inputs, // ๊ธฐ์กด์˜ input ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋’ค        [name]: value, // name ํ‚ค๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ value ๋กœ ์„ค์ •    });};

์ด ๋ถ€๋ถ„์„ ํ’€์–ด ์“ฐ๋ฉด

const nextInputs = {    ...inputs,};
nextInputs[name] = value;
setInputs(value);

useRef ๋กœ ํŠน์ • DOM ์„ ํƒํ•˜๊ธฐ#

JavaScript๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํŠน์ • DOM์„ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด getElementById , querySelector ๊ฐ™์€ DOM Selector ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ DOM์„ ์„ ํƒํ•œ๋‹ค.

๋ฆฌ์•กํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๊ฐ€๋” DOM์„ ์ง์ ‘ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜์žˆ๋Š”๋ฐ ๊ทธ๋Ÿด ๋•Œ ref ๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ref ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” useRef ๋ผ๋Š” Hook ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ React.createRef ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋Š” ํ˜„์žฌ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.

InputSample.js ์—์„œ ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ด๋ฆ„ input ์— ํฌ์ปค์Šค๊ฐ€ ์žกํžˆ๋„๋ก ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค.

InputSample.js

import React, {useState, useRef} from 'react';
function InputSample() {    const [inputs, setInputs] = useState({        name: '',        nickname: '',    });    const nameInput = useRef();
    const {name, nickname} = inputs; // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ํ†ตํ•ด ๊ฐ’ ์ถ”์ถœ
    const onChange = (e) => {        const {value, name} = e.target; // ์šฐ์„  e.target ์—์„œ name ๊ณผ value ๋ฅผ ์ถ”์ถœ        setInputs({            ...inputs, // ๊ธฐ์กด์˜ input ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋’ค            [name]: value, // name ํ‚ค๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ value ๋กœ ์„ค์ •        });    };
    const onReset = () => {        setInputs({            name: '',            nickname: '',        });        nameInput.current.focus();    };
    return (        <div>            <input                name="name"                placeholder="์ด๋ฆ„"                onChange={onChange}                value={name}                ref={nameInput}            />            <input                name="nickname"                placeholder="๋‹‰๋„ค์ž„"                onChange={onChange}                value={nickname}            />            <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>            <div>                <b>๊ฐ’: </b>                {name} ({nickname})            </div>        </div>    );}
export default InputSample;

React ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉฐ ๋Š๋‚€์ #

  • JS ๊ธฐ์ดˆ ๋ฌธ๋ฒ•๋งŒ ๋–ผ๊ณ  ๋ฐ”๋กœ React ์ž…๋ฌธ์œผ๋กœ ๋„˜์–ด์˜ค๋‹ˆ DOM, Event ๊ฐ์ฒด ๋“ฑ JS์—์„œ ๋ถ€์กฑํ–ˆ๋˜ ๊ฐœ๋…์ด ๋“ฑ์žฅํ•  ๋•Œ๋งˆ๋‹ค ๋‚ฏ์„  React ๊ตฌ์กฐ์™€ ํ•จ๊ป˜ ๋จธ๋ฆฌ ์†์„ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค .

  • ์•„์ง React์™€ JavaScript์˜ ๊ด€๊ณ„๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ฐ•์˜๋ฅผ ๋ณด๋ฉด JavaScript๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์™€ React๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„๋“ค์ด ์žˆ๋‹ค . React๋„ ๊ฒฐ๊ตญ JS๋‹ˆ๊นŒ JS๋กœ๋งŒ ํ•ด๋„ ๋‹ค ๋˜๋Š” ๊ฑธ๋กœ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์™œ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฑด์ง€ ์ดํ•ด๋˜์ง€ ์•Š๋Š”๋‹ค.

  • ์ข‹์€ React ๊ณต๋ถ€ ์ž๋ฃŒ๋ฅผ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค. ์ตœ์‹  ๊ธฐ์ˆ ์ด๋ผ ๋น ๋ฅด๊ฒŒ ๋ณ€ํ•˜๊ณ  ์žˆ์–ด์„œ ์ฑ…๋ณด๋‹ค๋Š” ์ธํ„ฐ๋„ท ์ž๋ฃŒ + ์˜จ๋ผ์ธ ๊ฐ•์˜๋กœ ๊ณต๋ถ€ํ•ด์•ผ ํ•œ๋‹ค.

    1. ๊ณต์‹ ๋ฌธ์„œ

    2. ์ƒํ™œ์ฝ”๋”ฉ - 4์‹œ๊ฐ„ 40๋ถ„ ๋ถ„๋Ÿ‰

    3. velopert ๋‹˜ ๊ฐ•์ขŒ - 2์‹œ๊ฐ„ 53๋ถ„ ๋ถ„๋Ÿ‰

    4. zeroCho ๋‹˜ ๊ฐ•์ขŒ - 12์‹œ๊ฐ„ 44๋ถ„ ๋ถ„๋Ÿ‰

    5. Udemy ๊ฐ•์ขŒ - 26์‹œ๊ฐ„ 42 ๋ถ„ ๋ถ„๋Ÿ‰


์•ž์œผ๋กœ ..#

  • zeroCho๋‹˜์˜ JavaScript ์›น๊ฒŒ์ž„ ๊ฐ•์ขŒ์™€ React ์›น๊ฒŒ์ž„ ๊ฐ•์ขŒ๊ฐ€ ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด์„œ๋„์ƒ๊ฐํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ด ๋‘ ๊ฐ•์ขŒ๋ฅผ ๋“ฃ๊ณ  ๊ธฐ์ดˆ๋ฅผ ์Œ“๋Š”๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค๋””์ž์ธ ์ˆ˜์—… ๋•Œ ๋งŒ๋“  ๋ฏธ๋””์–ดํ•™๊ณผ ์ˆ˜๊ฐ•๊ณ„ํš ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค. ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์ธ React๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์ข‹์€ ์˜ˆ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

ยท ์•ฝ 5๋ถ„

React#

props ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ (๊ณ„์†)#

defaultProps ๋กœ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •#

Hello.js

import React from 'react';
function Hello({color, name}) {    return <div style={{color}}>์•ˆ๋…•ํ•˜์„ธ์š” {name}</div>;}
Hello.defaultProps = {    name: '์ด๋ฆ„์—†์Œ',};
export default Hello;

์ด๋ ‡๊ฒŒ ์ปดํฌ๋„ŒํŠธ์— props ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜์žˆ๋‹ค.

App.js

import React from 'react';import Hello from './Hello';
function App() {    return (        <>            <Hello name="react" color="red" />            <Hello color="pink" />        </>    );}
export default App;

2020-03-15-200315-image-0

props.children#

์ปดํฌ๋„ŒํŠธ ํƒœ๊ทธ ์‚ฌ์ด์— ๋„ฃ์€ ๊ฐ’์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•, props.children ์„ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค.

props.children ์„ ์‚ฌ์šฉํ•˜๋Š” Wrapper.js ๋ฅผ ๋งŒ๋“ค์–ด ๋ณธ๋‹ค.

Wrapper.js

import React from 'react';
function Wrapper() {    const style = {        border: '2px solid black',        padding: '16px',    };    return <div style={style}></div>;}
export default Wrapper;

App.js

import React from 'react';import Hello from './Hello';import Wrapper from './Wrapper';
function App() {    return (        <Wrapper>            <Hello name="react" color="red" />            <Hello color="pink" />        </Wrapper>    );}
export default App;

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‚ด๋ถ€์˜ ๋‚ด์šฉ์ด ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. Wrapper.js ์—์„œ props.children ์„๋ Œ๋”๋งํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Wrapper.js

import React from 'react';
function Wrapper(props) {    // props ๋Œ€์‹  { children } ์œผ๋กœ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ํ›„    const style = {        border: '2px solid black',        padding: '16px',    };    return (        <div style={style}>            {props.children} {/* ์—ฌ๊ธฐ์„œ children ์œผ๋กœ๋งŒ ๋ฐ›์•„๋„ ๋œ๋‹ค. */}        </div>    );}
export default Wrapper;

2020-03-15-200315-image-1

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

props ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ Œ๋”๋ง ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

App.js

import React from 'react';import Hello from './Hello';import Wrapper from './Wrapper';
function App() {    return (        <Wrapper>            <Hello name="react" color="red" isSpecial={true} />            <Hello color="pink" />        </Wrapper>    );}
export default App;

isSpecial ์ด๋ผ๋Š” props ๋ฅผ ์ „๋‹ฌํ•˜๊ณ , (์ด ๋•Œ true ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ’์ด๋ฏ€๋กœ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผํ•œ๋‹ค.

Hello.js

import React from 'react';
function Hello({color, name, isSpecial}) {    return (        <div style={{color}}>            {isSpecial ? <b>*</b> : null}            ์•ˆ๋…•ํ•˜์„ธ์š” {name}        </div>    );}
Hello.defaultProps = {    name: '์ด๋ฆ„์—†์Œ',};
export default Hello;

์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•์œผ๋กœ isSpecial && <b>*</b> ๋กœ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋˜ํ•œ props ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ์ด๋ฆ„๋งŒ ์ž‘์„ฑํ•˜๊ณ  ๊ฐ’ ์„ค์ •์„ ์ƒ๋žตํ•  ๊ฒฝ์šฐ ์ด๋ฅผ true ๋กœ์„ค์ •ํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

App.js

import React from 'react';import Hello from './Hello';import Wrapper from './Wrapper';
function App() {    return (        <Wrapper>            <Hello name="react" color="red" isSpecial />            <Hello color="pink" />        </Wrapper>    );}
export default App;

Hello.js

import React from 'react';
function Hello({color, name, isSpecial}) {    return (        <div style={{color}}>            {isSpecial ? alert(isSpecial) : null}            ์•ˆ๋…•ํ•˜์„ธ์š”            {name}        </div>    );}
Hello.defaultProps = {    name: '์ด๋ฆ„์—†์Œ',};
export default Hello;

isSpecial ์— ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ  Hello.js ์—์„œ ๋งŒ์•ฝ isSpecial ์ด true ๋ผ๋ฉด isSpecial ์„ alert๋กœ ๋‚ด๋ณด๋‚ด๊ฒŒ ํ–ˆ๋‹ค.

2020-03-15-200315-image-2

true ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.


useState ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ”๋€Œ๋Š” ๊ฐ’ ๊ด€๋ฆฌํ•˜๊ธฐ#

Counter ์˜ˆ์ œ๋ฅผ react๋กœ ๊ตฌํ˜„ํ•ด ๋ณธ๋‹ค.

App.js

import React from 'react';import Counter from './Counter';
function App() {    return <Counter />;}
export default App;

Counter.js

import React, {useState} from 'react';
function Counter() {    const [number, setNumber] = useState(0);
    const onIncrease = () => {        setNumber(number + 1);    };
    const onDecrease = () => {        setNumber(number - 1);    };
    return (        <div>            <h1>{number}</h1>            <button onClick={onIncrease}>+1</button>            <button onClick={onDecrease}>-1</button>        </div>    );}
export default Counter;

ํ•จ์ˆ˜ํ˜• ์—…๋ฐ์ดํŠธ#

์ด ๋•Œ, setNumber ํ•จ์ˆ˜(Setter ํ•จ์ˆ˜)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์—…๋ฐ์ดํŠธ ํ•˜๊ณ  ์‹ถ์€ ์ƒˆ๋กœ์šด ๊ฐ’์„ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, ๊ทธ ๋Œ€์‹  ๊ธฐ์กด์˜ ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์—…๋ฐ์ดํŠธ ํ•  ์ง€์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ๋„ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Counter.js

import React, {useState} from 'react';
function Counter() {    const [number, setNumber] = useState(0);
    const onIncrease = () => {        setNumber((prevNumber) => prevNumber + 1);    };
    const onDecrease = () => {        setNumber((prevNumber) => prevNumber - 1);    };
    return (        <div>            <h1>{number}</h1>            <button onClick={onIncrease}>+1</button>            <button onClick={onDecrease}>-1</button>        </div>    );}
export default Counter;

ยท ์•ฝ 5๋ถ„

React#

๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ๋ฆฌ์•กํŠธ


React Component ๋งŒ๋“ค๊ธฐ#

Hello.js

import React from 'react';
function Hello() {    return <div>Hello World!</div>;}
export default Hello;

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ๋•

import React from 'react';

๋ฅผ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค.

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” XML ํ˜•์‹์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋ฅผ JSX ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

export default Hello;

์ด ์ฝ”๋“œ๋Š” Hello ๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๊ฒ ๋‹ค๋Š” ์˜๋ฏธ.

App.js

import React from 'react';import Hello from './Hello';
function App() {    return (        <div>            <Hello />        </div>    );}
export default App;

์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด, ( yarn start or npm start )

2020-03-14-200314-image-0

index.js

import React from 'react';import ReactDOM from 'react-dom';import './index.css';import App from './App';import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change// unregister() to register() below. Note this comes with some pitfalls.// Learn more about service workers: https://bit.ly/CRA-PWAserviceWorker.unregister();

์—ฌ๊ธฐ์„œ ReactDOM.render ๋ฅผ ๋ณด๋ฉด, id ๊ฐ€ root ์ธ DOM ๋‚ด๋ถ€์— ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ( <App /> )๋ฅผ ๋ Œ๋”๋งํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

์ด DOM์€ public/index.html ๋‚ด๋ถ€์— ์žˆ๋‹ค.

<div id="root"></div>

JSX#

JavaScript + XML, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ™•์žฅ ๋ฌธ๋ฒ•. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด๋ถ€์— ๋งˆํฌ์—… ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ. HTML ๊ฐ™์ด ์ƒ๊ฒผ์ง€๋งŒ ์‹ค์ œ๋ก  JS ์ด๋‹ค.

return <div>์•ˆ๋…•ํ•˜์„ธ์š”</div>;

Babel ์€ XML ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋ฅผ JS๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.

๊ทœ์น™#

  • ํƒœ๊ทธ๋ฅผ ์—ด์—ˆ์œผ๋ฉด ๋‹ซ์•„์•ผ ํ•จ. ( input , br ํฌํ•จ ๋ชจ๋“  ํƒœ๊ทธ. ํƒœ๊ทธ ์‚ฌ์ด์— ๋‚ด์šฉ์ด์—†์„ ๋•, <input/> ๋กœ ์…€ํ”„ํด๋กœ์ง• ๊ฐ€๋Šฅ)

  • ๋‘ ๊ฐœ ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค. <> </> Fragment๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ ํƒœ๊ทธ ์—†์ด ๋‘ ๊ฐœ ์ด์ƒ์˜ ํƒœ๊ทธ ๊ฐ€๋Šฅ.

  • JSX ๋‚ด๋ถ€์— ๋ณ€์ˆ˜๋Š” {} ๋กœ ๊ฐ์‹ธ์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Œ.


style ๊ณผ className#

style ๊ณผ CSS class ์„ค์ • ๋ฐฉ๋ฒ•์ด HTML๊ณผ ๋‹ค๋ฅด๋‹ค.

  • ์ธ๋ผ์ธ ์Šคํƒ€์ผ์€ ๊ฐ์ฒด์ฒ˜๋Ÿผ ์ž‘์„ฑ.

  • background-color ๊ฐ™์ด - ๋กœ ๊ตฌ๋ถ„๋œ ์†์„ฑ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์†์„ฑ๋“ค์€ camelCase ํ˜•ํƒœ๋กœ ( backgroundColor )

  • CSS class ์„ค์ •์‹œ์—” class="" ๊ฐ€ ์•„๋‹Œ className="" ์œผ๋กœ.

App.js

import React from 'react';import Hello from './Hello';import './App.css';
function App() {    const name = 'react';    const style = {        backgroundColor: 'black',        color: 'aqua',        fontSize: 24, // ๊ธฐ๋ณธ ๋‹จ์œ„ px        padding: '1rem', // ๋‹ค๋ฅธ ๋‹จ์œ„ ์‚ฌ์šฉ ์‹œ ๋ฌธ์ž์—ด๋กœ ์„ค์ •    };
    return (        <>            <Hello />            <div style={style}>{name}</div>            <div className="gray-box"></div>        </>    );}
export default App;

App.css

.gray-box {    background: gray;    width: 64px;    height: 64px;}

์ฃผ์„#

{/* JSX ๋‚ด๋ถ€ ์ฃผ์„ ์ž‘์„ฑ๋ฒ• */}

์—ด๋ฆฌ๋Š” ํƒœ๊ทธ ๋‚ด๋ถ€์—์„œ๋Š” // ์ด๋Ÿฐ ํ˜•ํƒœ์˜ ์ฃผ์„ ์ž‘์„ฑ๋„ ๊ฐ€๋Šฅ


props ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์— ๊ฐ’ ์ „๋‹ฌ#

App ์ปดํฌ๋„ŒํŠธ์—์„œ Hello ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ

App.js

import React from 'react';import Hello from './Hello';
function App() {    return <Hello name="react" />;}
export default App;

Hello.js

import React from 'react';
function Hello(props) {    return <div>์•ˆ๋…•ํ•˜์„ธ์š” {props.name}</div>;}
export default Hello;

props ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์กฐํšŒ, ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋˜๋ฏ€๋กœ props.name ์œผ๋กœ ์กฐํšŒ ๊ฐ€๋Šฅ


๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

App.js

import React from 'react';import Hello from './Hello';
function App() {    return <Hello name="react" color="red" />;}
export default App;

name , color ๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ์ „๋‹ฌ.

import React from 'react';
function Hello(props) {    return <div style={{color: props.color}}>์•ˆ๋…•ํ•˜์„ธ์š” {props.name}</div>;}
export default Hello;

color: props.color ๋Š” ๊ฐ์ฒด์ด๋ฏ€๋กœ {} ๋กœ ํ•œ ๋ฒˆ ๊ฐ์‹ธ๊ณ , ์ด ๊ฐ์ฒด๊ฐ€ JS ๊ฐ’์ด๋ฏ€๋กœ {} ๋กœ ํ•œ๋ฒˆ ๋” ๊ฐ์‹ผ๋‹ค.


๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ์ „๋‹ฌํ•  ๋•Œ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

import React from 'react';
function Hello({color, name}) {    return <div style={{color}}>์•ˆ๋…•ํ•˜์„ธ์š” {name}</div>;    // color: color -> color ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ}
export default Hello;

ยท ์•ฝ 9๋ถ„

React-Native ์‹คํ–‰ ํ™˜๊ฒฝ์„ค์ •#

๋งฅ(Mac)์— react native ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์ถ•ํ•˜๊ธฐ

์ด ๋ธ”๋กœ๊ทธ์˜ ์„ค์ • ๋ฐฉ๋ฒ•์„ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ React-Native ์‹คํ–‰ํ™˜๊ฒฝ์„ ์„ค์ •ํ–ˆ๋‹ค.


  ![2020-03-13-200313-image-0](./images/2020-03-13-200313-image-0.png)
  <br />


  ![2020-03-13-200313-image-1](./images/2020-03-13-200313-image-1.png)
  <br />

์„ฑ๊ณต!

JavaScript ๋ฌธ๋ฒ•#

์‚ผํ•ญ์—ฐ์‚ฐ์ž#

condition ? true : false

  • ํ•œ ์ค„์ด ๋„ˆ๋ฌด ๊ธธ ๋•Œ
let text = array.length === 0 ? `๋ฐฐ์—ด์ด ๋น„์–ด์žˆ๋‹ค.` : `๋ฐฐ์—ด์ด ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค`;

Truthy and Falsy#

undefined ์™€ null ์€ ๋ชจ๋‘ false ๋กœ ๊ฐ„์ฃผ๋จ.

Falsy ํ•œ ๊ฐ’

  • undefined

  • null

  • 0

  • 
    
  • NaN

  • false

์ด ์™ธ์—๋Š” ๋ชจ๋‘ true ๋กœ ๊ฐ„์ฃผ๋จ

! , !! ๋ฅผ ํ™œ์šฉํ•ด Falsy ํ•œ ๊ฐ’๊ณผ Truthy ํ•œ ๊ฐ’์„ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ


๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•#

A && B ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ A๊ฐ€ Truthy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, B๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋˜๊ณ , Falsy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, A๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

console.log(true && 'hello'); // helloconsole.log(false && 'hello'); // falseconsole.log('hello' && 'bye'); // byeconsole.log(null && 'hello'); // nullconsole.log(undefined && 'hello'); // undefinedconsole.log('' && 'hello'); // ''console.log(0 && 'hello'); // 0console.log(1 && 'hello'); // helloconsole.log(1 && 1); // 1

A || B ์—ฐ์‚ฐ์ž๋Š” ๋งŒ์•ฝ A๊ฐ€ Truthy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, A๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋˜๊ณ , Falsy ํ•œ๊ฐ’์ด๋ผ๋ฉด B๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

console.log(true || 'hello'); // trueconsole.log(false || 'hello'); // helloconsole.log('hello' || 'bye'); // helloconsole.log(null || 'hello'); // helloconsole.log(undefined || 'hello'); // undefinedconsole.log('' || 'hello'); // helloconsole.log(0 || 'hello'); // helloconsole.log(1 || 'hello'); // 1console.log(1 || 1); // 1

ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ#

ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ = ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

function calculateCircleArea(r = 1) {    return Math.PI * r * r;}
const area = calculateCircleArea();console.log(area); // 3.141592653589793

์กฐ๊ฑด๋ฌธ ๋” ์Šค๋งˆํŠธํ•˜๊ฒŒ ์“ฐ๊ธฐ#

  • ํŠน์ • ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐ’ ์ค‘ ํ•˜๋‚˜์ธ์ง€ ํ™•์ธํ•ด์•ผ ํ•  ๋•Œ
function isAnimal(text) {    return (        text === '๊ณ ์–‘์ด' || text === '๊ฐœ' || text === '๊ฑฐ๋ถ์ด' || text === '๋„ˆ๊ตฌ๋ฆฌ'    );}
console.log(isAnimal('๊ฐœ')); // trueconsole.log(isAnimal('๋…ธํŠธ๋ถ')); // false

๋ฐฐ์—ด์˜ includes ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ

function isAnimal(name) {    const animals = ['๊ณ ์–‘์ด', '๊ฐœ', '๊ฑฐ๋ถ์ด', '๋„ˆ๊ตฌ๋ฆฌ'];    return animals.includes(name);}
console.log(isAnimal('๊ฐœ')); // trueconsole.log(isAnimal('๋…ธํŠธ๋ถ')); // false

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

const isAnimal = (name) => ['๊ณ ์–‘์ด', '๊ฐœ', '๊ฑฐ๋ถ์ด', '๋„ˆ๊ตฌ๋ฆฌ'].includes(name);
console.log(isAnimal('๊ฐœ')); // trueconsole.log(isAnimal('๋…ธํŠธ๋ถ')); // false

  • ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•  ๋•Œ
function getSound(animal) {    if (animal === '๊ฐœ') return '๋ฉ๋ฉ!';    if (animal === '๊ณ ์–‘์ด') return '์•ผ์˜น~';    if (animal === '์ฐธ์ƒˆ') return '์งน์งน';    if (animal === '๋น„๋‘˜๊ธฐ') return '๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ';    return '...?';}

switch ์‚ฌ์šฉ

function getSound(animal) {    switch (animal) {        case '๊ฐœ':            return '๋ฉ๋ฉ!';        case '๊ณ ์–‘์ด':            return '์•ผ์˜น~';        case '์ฐธ์ƒˆ':            return '์งน์งน';        case '๋น„๋‘˜๊ธฐ':            return '๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ';        default:            return '...?';    }}
console.log(getSound('๊ฐœ')); // ๋ฉ๋ฉ!console.log(getSound('๋น„๋‘˜๊ธฐ')); // ๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ

๊ฐ์ฒด ํ™œ์šฉ

function getSound(animal) {    const sounds = {        ๊ฐœ: '๋ฉ๋ฉ!',        ๊ณ ์–‘์ด: '์•ผ์˜น~',        ์ฐธ์ƒˆ: '์งน์งน',        ๋น„๋‘˜๊ธฐ: '๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ',    };    return sounds[animal] || '...?';}
console.log(getSound('๊ฐœ')); // ๋ฉ๋ฉ!console.log(getSound('๋น„๋‘˜๊ธฐ')); // ๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ

  • ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฝ”๋“œ ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ
function makeSound(animal) {    const tasks = {        ๊ฐœ() {            console.log('๋ฉ๋ฉ');        },        ๊ณ ์–‘์ด() {            console.log('๊ณ ์–‘์ด');        },        ๋น„๋‘˜๊ธฐ() {            console.log('๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ');        },    };    if (!tasks[animal]) {        console.log('...?');        return;    }    tasks[animal]();}
makeSound('๊ฐœ');makeSound('๋น„๋‘˜๊ธฐ');

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น (๊ตฌ์กฐ๋ถ„ํ•ด) ๋ฌธ๋ฒ•#

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

const object = {a: 1, b: 2};
const {a, b} = object;
console.log(a); // 1console.log(b); // 2
  • ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ
const object = {a: 1, b: 2};
function print({a, b}) {    console.log(a);    console.log(b);}
print(object);

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์‹œ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •#

const object = {a: 1};
const {a, b = 2} = object;
console.log(a); // 1console.log(b); // 2
  • ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ
const object = {a: 1};
function print({a, b = 2}) {    console.log(a);    console.log(b);}
print(object);// 1// 2

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์‹œ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ#

const animal = {    name: '๋ฉ๋ฉ์ด',    type: '๊ฐœ',};
const nickname = animal.name;
console.log(nickname); // ๋ฉ๋ฉ์ด

animal.name ๊ฐ’์„ nickname ๊ฐ’์— ๋‹ด๊ณ  ์žˆ๋‹ค. ์ด๋ฅผ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด,

const animal = {    name: '๋ฉ๋ฉ์ด',    type: '๊ฐœ',};
const {name: nickname} = animal;console.log(nickname);

: ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ๋‹ค.


๋ฐฐ์—ด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

const array = [1, 2];const [one, two] = array;
console.log(one);console.log(two);

๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์›์†Œ๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์ƒˆ๋กœ ์„ ์–ธํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉ. ๊ฐ์ฒด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •์ด ๊ฐ€๋Šฅ.

const array = [1];const [one, two = 2] = array;
console.log(one);console.log(two);

๊นŠ์€ ๊ฐ’ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

const deepObject = {    state: {        information: {            name: 'younho9',            languages: ['korean', 'english', 'chinese'],        },    },    value: 5,};

์—ฌ๊ธฐ์„œ name , languages , value ๊ฐ’์„ ๋ฐ–์œผ๋กœ ๊บผ๋‚ด๊ณ  ์‹ถ์„ ๋•Œ

const deepObject = {    state: {        information: {            name: 'younho9',            languages: ['korean', 'english', 'chinese'],        },    },    value: 5,};
const {name, languages} = deepObject.state.information;const {value} = deepObject;
const extracted = {    name,    languages,    value,};
console.log(extracted); // {name: "younho9", languages: Array[3], value: 5}

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

const extracted = {    name: name,    languages: languages,    value: value,};

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€

const deepObject = {    state: {        information: {            name: 'younho9',            languages: ['korean', 'english', 'chinese'],        },    },    value: 5,};
const {    state: {        information: {name, languages},    },    value,} = deepObject;
const extracted = {    name,    languages,    value,};
console.log(extracted);

์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋‹ค๋ฃจ๊ธฐ#

2020-03-13-200313-image-2

function work() {    const start = Date.now();    for (let i = 0; i < 10000000; i++) {}    const end = Date.now();    console.log(end - start + 'ms');}
work();console.log('๋‹ค์Œ ์ž‘์—…');

work() ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์•ˆ ๋‹ค์Œ ์ž‘์—…์ด ์ง„ํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

์ด๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค. โ†’ setTImeout() ํ•จ์ˆ˜ ์‚ฌ์šฉ

function work() {    setTimeout(() => {        const start = Date.now();        for (let i = 0; i < 1000000000; i++) {}        const end = Date.now();        console.log(end - start + 'ms');    }, 0);}
console.log('์ž‘์—… ์‹œ์ž‘!');work();console.log('๋‹ค์Œ ์ž‘์—…');

๋จผ์ € work() ์ดํ›„์˜ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณ  work() ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•œ๋‹ค.

๋งŒ์•ฝ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ work() ํ•จ์ˆ˜๊ฐ€ ๋๋‚œ ์ดํ›„์— ์–ด๋–ค ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

function work(callback) {    setTimeout(() => {        const start = Date.now();        for (let i = 0; i < 1000000000; i++) {}        const end = Date.now();        console.log(end - start + 'ms');        callback();    }, 0);}
console.log('์ž‘์—… ์‹œ์ž‘!');work(() => {    console.log('์ž‘์—…์ด ๋๋‚ฌ์–ด์š”!');});console.log('๋‹ค์Œ ์ž‘์—…');

work() ๊ฐ€ ๋๋‚œ ๋’ค์— ์ˆ˜ํ–‰ํ•  ํ•จ์ˆ˜(์ž‘์—…)๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ค€๋‹ค.


Promise#

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋งŽ์•„์งˆ ๋•Œ, ๋ชจ๋‘ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋‚œ์žกํ•ด์ง€๊ฒŒ๋œ๋‹ค.

function increaseAndPrint(n, callback) {    setTimeout(() => {        const increased = n + 1;        console.log(increased);        if (callback) {            callback(increased);        }    }, 1000);}
increaseAndPrint(0, (n) => {    increaseAndPrint(n, (n) => {        increaseAndPrint(n, (n) => {            increaseAndPrint(n, (n) => {                increaseAndPrint(n, (n) => {                    console.log('๋!');                });            });        });    });});

์ด๋ฅผ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ES6์— ๋„์ž…๋œ ๊ธฐ๋Šฅ์ด Promise ์ด๋‹ค.

Promise ๋Š” ์„ฑ๊ณตํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์„ฑ๊ณตํ•  ๋•Œ๋Š” resolve ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๊ณ , ์‹คํŒจํ•  ๋•Œ๋Š” reject ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

const myPromise = new Promise((resolve, reject) => {    setTimeout(() => {        resolve(1);    }, 1000);});
myPromise.then((n) => {    console.log(n);});

ยท ์•ฝ 6๋ถ„

๊ฐ์ฒด์ง€ํ–ฅ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ#

MDN ๋ฌธ์„œ

ํ•จ์ˆ˜ ๋ณต์Šต#

ํ•จ์ˆ˜ํ‘œํ˜„์‹์—์„œ ๊ธฐ๋ช…ํ•จ์ˆ˜์™€ ์ต๋ช…ํ•จ์ˆ˜


var add = function sum(a, b) {    return a + b;};

์—ฌ๊ธฐ์—์„œ sum ๊ฐ€ ํ•จ์ˆ˜ ์ด๋ฆ„, add ์ด ํ•จ์ˆ˜ ๋ณ€์ˆ˜.

ํ•จ์ˆ˜ ์ด๋ฆ„์€ ์™ธ๋ถ€ ์ฝ”๋“œ์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€. (ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์žฌ๊ท€ ํ˜ธ์ถœ or ๋””๋ฒ„๊ฑฐ์—์„œ ํ•จ์ˆ˜ ๊ตฌ๋ถ„์„ ์œ„ํ•ด ์‚ฌ์šฉ)

ํ•จ์ˆ˜์„ ์–ธ๋ฌธ์€ ํ•จ์ˆ˜ ์ด๋ฆ„๊ณผ ํ•จ์ˆ˜ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค


ํ•จ์ˆ˜ ๊ฐ์ฒด์˜ ๊ธฐ๋ณธ ํ”„๋กœํผํ‹ฐ#


์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์„œ ์œ„์—์„œ ์ •์˜ํ•œ add ํ•จ์ˆ˜์˜ ํ”„๋กœํผํ‹ฐ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.


console.dir(add);

caller : ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅ. (์ „์—ญ์ธ ๊ฒฝ์šฐ null )


function some() {    return foo();}
function foo() {    var a = 3;    return bar();}
function bar() {    return 'caller : ' + bar.caller;}
console.log(foo(bar)); // caller : function foo(func) {...}console.log(bar()); // null (browser์—์„œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ)

arguments : ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌ๋œ ์ธ์ž๊ฐ’.

arguments ๊ฐ์ฒด๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋“ค์˜

function multiply(x, y) {    console.log(arguments);    return x * y;}
multiply(); // {}multiply(1); // { '0': 1 }multiply(1, 2); // { '0': 1, '1': 2 }multiply(1, 2, 3); // { '0': 1, '1': 2, '2': 3 }

__proto__ : ์ž์‹ ์˜ ๋ถ€๋ชจ ์—ญํ• ์„ ํ•˜๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด. ํ•จ์ˆ˜ ๊ฐ์ฒด์˜ ๋ถ€๋ชจ๋Š” Function.prototype ๊ฐ์ฒด

length : ํ•จ์ˆ˜ ์ •์˜ ์‹œ ์ž‘์„ฑ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐฏ์ˆ˜


function foo() {}console.log(foo.length); // 0
function bar(x) {    return x;}console.log(bar.length); // 1
function baz(x, y) {    return x * y;}console.log(baz.length); // 2

prototype : ์œ„์˜ __proto__ ์™€ ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋กœ ์ด ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ์ž๋กœ ์‚ฌ์šฉ๋  ๋•Œ์ดํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ๋ถ€๋ชจ ์—ญํ• ์„ ํ•˜๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.


์ฝœ๋ฐฑ ํ•จ์ˆ˜#

๊ฐœ๋ฐœ์ž๋Š” ๋‹จ์ง€ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ฑฐ๋‚˜ ํŠน์ • ์‹œ์ ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ์‹œ์Šคํ…œ์—์„œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜. (๋Œ€ํ‘œ์ ์œผ๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ฒ˜๋ฆฌ)


function initTmap() {    var map = new Tmapv2.Map(        'map', // "map" : ์ง€๋„๊ฐ€ ํ‘œ์‹œ๋  div์˜ id        {            center: new Tmapv2.LatLng(37.566481622437934, 126.98502302169841), // ์ง€๋„ ์ดˆ๊ธฐ ์ขŒํ‘œ            width: '100%', // map์˜ width ์„ค์ •            height: '452px', // map์˜ height ์„ค์ •            zoom: 19,        },    );    //๋‹ค๊ฐํ˜•    var polygon = new Tmapv2.Polygon({        paths: [            new Tmapv2.LatLng(37.56661, 126.985666),            new Tmapv2.LatLng(37.566595, 126.985985),            new Tmapv2.LatLng(37.566512, 126.986071),            new Tmapv2.LatLng(37.566397, 126.985894),            new Tmapv2.LatLng(37.566395, 126.985664),        ],        fillColor: 'pink',        draggable: true,        map: map,    });}
window.onload = initTmap;

์–ด์ œ ๊ณต๋ถ€ํ–ˆ๋˜ Tmap API, ๋งˆ์ง€๋ง‰์ค„์˜ window.onload = initTmap ๋กœ ์œ„์—์„œ ์ •์˜ํ•œ initTmap ํ•จ์ˆ˜๋ฅผ onload ์‹œ(์›น ํŽ˜์ด์ง€์˜ ๋กœ๋”ฉ์ด ๋๋‚˜๋Š” ์‹œ์ )์— ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.

์ด๋Ÿฌํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ๋•Œ, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ต๋ช… ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค.


<!DOCTYPE html><html>    <body>        <script>            window.onload = function () {                alert('This is the callback function.');            };        </script>    </body></html>

์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜#

ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•จ๊ณผ ๋™์‹œ์— ๋ฐ”๋กœ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜.


(function (name) {    console.log('This is the immediate function --> ' + name);})('foo');

ํ•จ์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด์„ ๊ด„ํ˜ธ () ๋กœ ๊ฐ์‹ธ๊ณ  ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๊ฒŒ ๋์— () ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ด ๊ด„ํ˜ธ ์•ˆ์— ์ธ์ž๋ฅผ ๋„˜๊ธธ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜๋Š” ์ตœ์ดˆ ํ•œ ๋ฒˆ์˜ ์‹คํ–‰๋งŒ์„ ํ•„์š”๋กœ ํ•˜๋Š” ์ดˆ๊ธฐํ™” ์ฝ”๋“œ ๋ถ€๋ถ„์— ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋‹ค. (๋ณ€์ˆ˜๋“ค์˜ ์œ ํšจ ๋ฒ”์œ„๋ฅผ ์ „์—ญ์ด ์•„๋‹ˆ๋ผ ๋‚ด๋ถ€ ํ•จ์ˆ˜๋กœ ๊ฐ์ถ”๋Š” ํšจ๊ณผ)

์ฆ‰์‹œ ์‹คํ–‰ํ•จ์ˆ˜๋Š” ์ตœ์ดˆ ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋˜๋ฉฐ ๋‹ค์‹œ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.


๋‚ด๋ถ€ ํ•จ์ˆ˜#

ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ •์˜๋œ ํ•จ์ˆ˜.

ํด๋กœ์ € ์ƒ์„ฑ์ด๋‚˜ ์™ธ๋ถ€์—์„œ์˜ ์ ‘๊ทผ์„ ๋ง‰๊ณ  ๋…๋ฆฝ์ ์ธ ํ—ฌํผ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ

  • ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ๋Š” ์ž์‹ ์„ ๋‘˜๋Ÿฌ์‹ผ ๋ถ€๋ชจ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผ ๊ฐ€๋Šฅ

  • ๋‚ด๋ถ€ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์‹ ์ด ์ •์˜๋œ ๋ถ€๋ชจ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ํ˜ธ์ถœ ๊ฐ€๋Šฅ

์™ธ๋ถ€์—์„œ ํŠน์ • ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๋ถ€๋ชจ ํ•จ์ˆ˜์—์„œ ๋‚ด๋ถ€ ํ•จ์ˆ˜๋ฅผ ์™ธ๋ถ€๋กœ ๋ฆฌํ„ด. (ํด๋กœ์ €)