HomeReact & Next.js ๊ฒจ์šฐ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ2ํƒ„

2ํƒ„

4 min read

webdev

1ํƒ„

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

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์›นํŽ˜์ด์ง€๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ์–ธ์–ด์ด๋‹ค.

๊ธฐ์กด์— ํŒŒ์ด์ฌ์ด๋‚˜ C์–ธ์–ด ๊ฐ™์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•์€ ๋ณ„๋กœ ์–ด์ƒ‰ํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค.

ํ•œ ์˜ˆ๋กœ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ˆ„๋ฅธ ํšŸ์ˆ˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์›นํŽ˜์ด์ง€๋ฅผ ์ง์ ‘ ์ œ์ž‘ํ•ด๋ณด์ž.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>๋”ธ๊น</title>
</head>
<body>
    <button id="alertButton">ํƒœ์ผ์ด ๋•Œ๋ฆฌ๊ธฐ</button>

	<span>ํƒœ์ผ์ด๋ฅผ 0๋ฒˆ ๋•Œ๋ €์–ด์š”! </span>

    <script> 
		...
    </script>
</body>
</html>

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์€ CSS ํŒŒ์ผ๊ณผ ๊ฐ™์ด ์™ธ๋ถ€ ํŒŒ์ผ์„ ๋กœ๋”ฉํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด HTMLํŒŒ์ผ ๋‚ด๋ถ€์—์„œ script ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ์ •์˜ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.

~~๋”ธ๊น~~

์ด์ œ script ํƒœ๊ทธ ์•ˆ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>๋”ธ๊น</title>
</head>
<body>
    <button id="alertButton">ํƒœ์ผ์ด ๋•Œ๋ฆฌ๊ธฐ</button>
    <span id="clickCount">ํƒœ์ผ์ด๋ฅผ 0๋ฒˆ ๋•Œ๋ ธ์–ด์š”! </span>

    <script>
        // ํด๋ฆญ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
        let count = 0;

        // ๋ฒ„ํŠผ๊ณผ span ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ
        const button = document.getElementById('alertButton');
        const clickCountText = document.getElementById('clickCount');

        // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‹คํ–‰๋  ํ•จ์ˆ˜
        button.addEventListener('click', () => {
            // ํด๋ฆญ ํšŸ์ˆ˜ ์ฆ๊ฐ€
            count++;
            // ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ์„ค์ •
            clickCountText.textContent = `ํƒœ์ผ์ด๋ฅผ ${count}๋ฒˆ ๋•Œ๋ ธ์–ด์š”!`;
        });

		alert('โ™ก')
	</script>
</body>
</html>

์œ„ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ํด๋ฆญ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”:

    let count = 0;
    
  2. HTML ์š”์†Œ๋ฅผ JavaScript์—์„œ selectํ•˜๊ธฐ

    • ๋ฒ„ํŠผ ์š”์†Œ๋ฅผ getElementById๋ฅผ ํ†ตํ•ด ์„ ํƒํ•œ๋‹ค.
    • ํด๋ฆญ ํšŸ์ˆ˜๋ฅผ ํ‘œ์‹œํ•  span ์š”์†Œ๋ฅผ getElementById๋ฅผ ํ†ตํ•ด ์„ ํƒํ•œ๋‹ค.
    const button = document.getElementById('alertButton');
    const clickCountText = document.getElementById('clickCount');
    
  3. ๋ฒ„ํŠผ์— ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ

    • ๋ฒ„ํŠผ์ด ํด๋ฆญ๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋  ํ•จ์ˆ˜๋ฅผ addEventListener๋กœ ์„ค์ •ํ•œ๋‹ค.
    • ์ด ํ•จ์ˆ˜๋Š” ํด๋ฆญ ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , span ์š”์†Œ์˜ ํ…์ŠคํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
    button.addEventListener('click', () => {
        count++;
        clickCountText.textContent = `ํƒœ์ผ์ด๋ฅผ ${count}๋ฒˆ ๋•Œ๋ ธ์–ด์š”!`;
    });
    

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ํ…์ŠคํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜์–ด ํด๋ฆญ ํšŸ์ˆ˜๊ฐ€ ๋ฐ˜์˜๋œ๋‹ค.

์—ฌ๊ธฐ์„œ,

(a,b) => { 
	...
}

๋Š” arrow function์ด๋ผ ๋ถ€๋ฅด๋Š”๋ฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

function asdf(a,b){
	....
}

๋ฌผ๋ก  ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•ด๋„ ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์—์„œ๋Š” ํฐ ์ฐจ์ด๋Š” ์—†๋‹ค. ํ•˜์ง€๋งŒ, ์ด๋ฆ„์ด ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๊ณค ํ•œ๋‹ค.

์•ˆ์ง€ํ˜ธ์˜ React & Next.js ๊ฒจ์šฐ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ - 3ํƒ„


Links to this note

hi

React & Next.js ๊ฒจ์šฐ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ/2ํƒ„ **

1ํƒ„

์—ฌ๊ธฐ์„œ ๋ณด์ž 2ํƒ„ |2ํƒ„์œผ๋กœ๊ฐ‘์‹œ๋‹ค**