๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

WINK-(Web & App)/HTML & CSS & JS ์Šคํ„ฐ๋””

[2023 ์‹ ์ž…๋ถ€์› ์‹ฌํ™” ์Šคํ„ฐ๋””] ์ •ํ˜ธ์šฉ #5์ฃผ์ฐจ react - section 0 ~ 2

๋ฐ˜์‘ํ˜•

๐Ÿ“Œsection 0
์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ -> React์—์„œ๋Š” Component๋ผ๊ณ  ํ•จ. 
1. ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค.
2. ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’๋‹ค. -> ํ•œ ๋ฒˆ ์ •์˜ํ•ด ๋‘๋ฉด ๋‹ค๋ฅธ ๊ณณ์—์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
3. ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์ข‹๋‹ค. -> ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ์ด ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.

๐Ÿ“Œsection 2

๐ŸŽฎ1. ๋ฆฌ์•กํŠธ๊ฐ€ ์—†๋‹ค๋ฉด?
public ๋””๋ ‰ํ† ๋ฆฌ -> npm run start ํ–ˆ์„ ๋•Œ ํŒŒ์ผ์„ ์ฐพ๋Š” ๋””๋ ‰ํ† ๋ฆฌ
semantic code <header></header> ๋‚˜ <nav></nav>์™€ ๊ฐ™์ด ๊ธฐ๋Šฅ์€ ์—†์ง€๋งŒ ์˜๋ฏธ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๊ตฌ๋ถ„ํ•œ ์ฝ”๋“œ
๋งŒ์•ฝ html ์ฝ”๋“œ๊ฐ€ 1000๋งŒ์ค„, 1์–ต์ค„์ด๋ผ๋ฉด ์ฝ”๋“œ๊ฐ€ ํ•œ๋ˆˆ์— ์•ˆ ๋“ค์–ด์˜จ๋‹ค. -> ํ˜ผ๋ž€์Šค๋Ÿฌ์›€
์ฝ”๋“œ๋ฅผ ์•ˆ๋ณด์ด๊ฒŒ ๊ฐ์ถฐ๋‘๊ณ  ์‹ฌํ”Œํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค! -> ๊ทธ๊ฑธ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฆฌ์•กํŠธ

 

๐ŸŽฎ2. ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ 1

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


์œ„ ์ฝ”๋“œ๋ฅผ Subject๋ผ๋Š” ํƒœ๊ทธ๋กœ ๋ฐ”๊พธ๊ธฐ

 

class Subject extends Compoent{
    render(){ -> javascript์˜ ์ตœ์‹ ๋ฒ„์ „์—์„œ๋Š” function ์ƒ๋žต
        return (
          <h1>WEB</h1> -> ์ฃผ์˜์  : ํ•˜๋‚˜์˜ ์ตœ์ƒ์œ„ ํƒœ๊ทธ๋งŒ ์จ์•ผ ํ•จ
          world wide web!
        );
    }
}



๊ฒฐ๊ณผ :

import React, {Component} from 'react';
import './App.css';
class App extends Component{
render(){
    return (
        <div className='App'>
            <Subject></Subject> -> ๋ฐฉ๊ธˆ ์œ„ ์ฝ”๋“œ๋ฅผ subject๋กœ ๋ฐ”๊ฟ”์ค€ ๊ฒƒ์ž„
        </div>
    )
}



์œ„ ์ฝ”๋“œ๋“ค์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ฐ€ ์•„๋‹˜! ์‹ค์ œ๋กœ ์“ฐ๋ฉด ๋ฌธ๋ฒ•์˜ค๋ฅ˜ ๋‚จ
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋‚˜ ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด 
jsx๋ผ๋Š” ๊ฒƒ์„ ๋งŒ๋“ ๊ฒƒ์ž„.


๐ŸŽฎ3. ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ2

 

class TOC extends Component{
    render(){
        return (
            <nav>
                <li><a href="1.html">HTML</a></li>
                <li><a href="2.html">CSS</a></li>
                <li><a href="3.html">Javascript</a></li>
            </nav>
        );
    }
}

class App extends Component{
    render(){
        return (
            <div className='App'>
                <Subject></Subject> 
                <TOC></TOC>
                <Content></Content>
            </div>
        )
    }
}

class Content extends Component{
    render(){
        return(
            <article>
                <h2>HTML</h2>
                HTML is HyperText Markup Language.
            </article>
        );
    }
}


๋ฆฌ์•กํŠธ๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” ์ฒซ ๋ฒˆ์งธ ์‹œ์„  -> ์ •๋ฆฌ์ •๋ˆ ํ•ด ์ฃผ๋Š” ๋„๊ตฌ
์ •๋ฆฌ์ •๋ˆ ํ›„ ์ด๋ฆ„ํ‘œ๋ฅผ ๋ถ™์ด๋ฉด ์ด๋ฆ„๋งŒ ๊ธฐ์–ตํ•˜๋ฉด ๋จ
๋ฆฌ์•กํŠธ๋„ ๋™์ผํ•จ
์ฝ”๋“œ ์ •๋ฆฌ ํ›„ ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„๋งŒ ๋ถ™์ด๋ฉด ๊ทธ๊ฒƒ๋งŒ ๊ธฐ์–ตํ•˜๋ฉด ๋จ


๐ŸŽฎ4. props


์ด๊ฑธ ๊ทธ๋Œ€๋กœ ์ธํ„ฐ๋„ท์— ์˜ฌ๋ฆฌ๋ฉด ์‚ฌ๋žŒ๋“ค์€ ์ด ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ๊ณณ์— ๋ถ™์—ฌ์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.App
๊ทธ๋Ÿฐ๋ฐ ํ•ญ์ƒ ๋˜‘๊ฐ™์€ ๊ฒฐ๊ณผ๋ฌผ์„ ๋‚ด๋†“๋Š”๋‹ค! -> ๋ฌด์–ธ๊ฐ€ ์•„์‰ฝ๋‹ค
ex) <a href=""></a>์€ attribute(์†์„ฑ) ์„ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ์ฃผ์†Œ๋ฅผ ํ‘œํ˜„ํ•จ
๊ทผ๋ฐ ํ•ญ์ƒ ๋˜‘๊ฐ™์€ ์ฃผ์†Œ๋กœ๋งŒ ์—ฐ๊ฒฐ๋œ๋‹ค!

class App extends Component{
    render(){
        return (
            <div className='App'>
                <Subject title="WEB" sub="world wide web!"></Subject> 
                <Subject title="React" sub="For UI"></Subject> 
                <TOC></TOC>
                <Content title="HTML" desc ="HTML is HyperText Markup Language."></Content>
            </div>
        )
    }
}

title์„ javascript๋Š” attribute๋ผ๊ณ  ํ•˜์ง€๋งŒ, react์—์„œ๋Š” props๋ผ๊ณ  ํ•จ

class Subject extends Compoent{
    render(){ 
        return (
          <h1>{this.props.title}</h1> 
          {this.props.sub}
        );
    }
}
class Content extends Component{
    render(){
        return(
            <article>
                <h2>{this.props.title}</h2>
                {this.props.desc}
            </article>
        );
    }
}


์œ„ ์ฝ”๋“œ๋Š” ์•ž์ „ ์ฝ”๋“œ์™€ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜์ง€๋งŒ ์ฝ”๋“œ๋Š” ํšจ์œจ์ ์ด๊ณ , ๊น”๋”ํ•ด์กŒ๋‹ค -> ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง์ด๋ผ๊ณ  ํ•จ.App
title๊ณผ sub์ด subject๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ž…๋ ฅ๊ฐ’์ด ๋˜์–ด์„œ ๊ทธ์— ๋”ฐ๋ผ ์ถœ๋ ฅ๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ํ•จ
์™„๋ฒฝํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ ์™„์„ฑ~!

 


๐ŸŽฎ5. React Developer Tools


react ํ™ˆํŽ˜์ด์ง€ -> community -> tools -> debugging
ํ˜„์žฌ ๋ฆฌ์•กํŠธ๋กœ ๋งŒ๋“ค์–ด์ง„ ์•ฑ๋“ค์˜ ์ƒํƒœ๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅ 
react developer tools ์„ค์น˜
์›ํ•˜๋Š” ํŽ˜์ด์ง€ -> ๊ฐœ๋ฐœ์ž ๋„๊ตฌ -> elements ํด๋ฆญ
<Subject title="WEB"></Subject>์˜ ์˜์—ญ์€ <h1>WEB</h1> ์ด ๋ถ€๋ถ„์ด์—ˆ๋‹ค.
react ํƒญ ํด๋ฆญ
react๋กœ ๋งŒ๋“  ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ

ํƒœ๊ทธ ๋ถ„์„ -> elementsํƒญ
์ปดํฌ๋„ŒํŠธ ๋ถ„์„ -> reactํƒญ
react ํƒญ์—์„œ ๊ฐ’์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.


๐ŸŽฎ6. Component ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ


ํ•œ ํŒŒ์ผ ์•ˆ์— ์ปดํฌ๋„ŒํŠธ๊ฐ€ 1000๊ฐœ๋ผ๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.
๋„ˆ๋ฌด ๋งŽ๊ธฐ๋„ ํ•˜๊ณ  ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๋ถˆ๋Ÿฌ์“ธ ์ˆ˜๋„ ์—†๋‹ค.
์ปดํฌ๋„ŒํŠธ๋ฅผ ํŒŒ์ผ๋ณ„๋กœ ๋‚˜๋ˆ ๋ณด์ž.



** App.js **

class Content extends Component{
    render(){
        return(
            <article>
                <h2>HTML</h2>
                HTML is HyperText Markup Language.
            </article>
        );
    }
}


** TOC.js **

import React, {Component} from 'react'; -> ์ด๊ฑธ ์”€์œผ๋กœ์จ component๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
class TOC extends Component{
    render(){
        return (
            <nav>
                <li><a href="1.html">HTML</a></li>
                <li><a href="2.html">CSS</a></li>
                <li><a href="3.html">Javascript</a></li>
            </nav>
        );
    }
}

export default TOC;
TOC๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์™ธ๋ถ€์—์„œ ๊ฐ–๋‹ค์“ธ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ

import TOC from "./components/TOC";
์™ธ๋ถ€์—์„œ TOC๋ฅผ ๊ฐ–๋‹ค ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋จ



** Content.js **

import React, {Component} from 'react';
class Content extends Component{
    render(){
        return(
            <article>
                <h2>{this.props.title}</h2>
                {this.props.desc}
            </article>
        );
    }
}

export default Content;


๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ ์‹œ :
import Content from "./components/Content";


ํ•„์š”ํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŽฎ๊ฐœ์ธ์ ์ธ ๊ถ๊ธˆ์ฆ
์ฒ˜์Œ ํด๋ž˜์Šค ์„ ์–ธ ์‹œ ์™œ render()๋ฅผ ์“ฐ๋Š” ๊ฑธ๊นŒ?

-> render()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋Š” ์‹ค์ œ dom์„ ์˜๋ฏธํ•˜์ง€ ์•Š๊ณ  ๊ทธ ํ˜•ํƒœ๋ฅผ ๋ณธ๋”ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

-> ์ฐธ๊ณ  ์‚ฌ์ดํŠธ
https://seungddak.tistory.com/166

 

[react] render ํ•จ์ˆ˜ ์™€ ๊ฐ€์ƒ๋”์— ๋Œ€ํ•˜์—ฌ

- ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ Œ๋”ํ•จ์ˆ˜ ์•ˆ์˜ jsx ๋Š” ์‹ค์ œ Dom ์„ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ€์ƒ์˜ Dom ์„ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. - render() ์˜ ๊ฒฐ๊ณผ๋Š” ์‹ค์ œdom ์„ ์˜๋ฏธํ•˜์ง€ ์•Š๊ณ  ๊ทธํ˜•ํƒœ๋ฅผ ๋ณธ๋”ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ

seungddak.tistory.com

 

-> ์Šคํ„ฐ๋”” ํ›„ ์ •๋ช…๋‹˜์˜ ๋‹ต๋ณ€ 

 

๋ฆฌ์•กํŠธ -> ๊ฐ€์ƒ domํ™œ์šฉ
div ์•ˆ์˜ ๋‚ด์šฉ๋“ค์€ dom์˜ ํ˜•ํƒœ๋ฅผ ๋„๊ณ  ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ˜•ํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
html์˜ ์ •์ ์ธ ๊ฒƒ๋“ค์„ js react๋ฅผ ์ด์šฉํ•ด์„œ ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค
์ƒํ™ฉ๋ณ„๋กœ ๋ Œ๋”๋ง์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค 
ex) ๋กœ๊ทธ์ธ ์ฐฝ์—์„œ ์•„์ด๋”” ์ž…๋ ฅ ์•ˆํ–ˆ์„ ๋•Œ ์•„์ด๋”” ์ž…๋ ฅํ•˜๋ผ๊ณ  ๋‚˜์˜ค๋Š” ํ™”๋ฉด๋„ react๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์ž„.

๋ฐ˜์‘ํ˜•