๐Ÿ’ช React. Class VS Function Style Component w/ ์ƒํ™œ์ฝ”๋”ฉ

[meษช]ยท2021๋…„ 10์›” 23์ผ
0

1-3. Today I Learned. React.js

๋ชฉ๋ก ๋ณด๊ธฐ
8/9

๐Ÿ“Œ ๊ฐœ๋ฐœ ๊ณต๋ถ€ 3๊ฐœ์›” ์ฐจ์ธ to-be ๊ฐœ๋ฐœ์ž์˜ ์ž์Šต ๋ธ”๋กœ๊ทธ๐Ÿ™‚๏พ Oct 17 ~ 23, 2021

ํ˜„์žฌ ์ƒํƒœ

class์™€ function์„ ๋”ฐ๋กœ ํ•™์Šตํ–ˆ๋Š”๋ฐ, ๊ฐ™์ด ๋†“๊ณ  ๋น„๊ตํ•˜๋ฉด์„œ ํ•™์Šตํ•ด์•ผ ๊ฐœ๋…์ด ๋” ์ž˜ ์žกํž ๊ฒƒ ๊ฐ™์•„์„œ.

intro

๋ฆฌ์•กํŠธ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.
ํด๋ž˜์Šค & ํ•จ์ˆ˜!

์˜ˆ์ „์—๋Š” ํ•จ์ˆ˜ ์Šคํƒ€์ผ๋กœ state๋ฅผ ๊ด€๋ฆฌํ•˜์ง€ ๋ชปํ–ˆ๊ณ  ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ๋ณ€๊ฒฝ ์†Œ๋ฉธ์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ์ธ ๋ผ์ดํ”„์‚ฌ์ดํด api๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์—ˆ์Œ
๊ทธ๋ž˜์„œ ํ•จ์ˆ˜ ์Šคํƒ€์ผ์€ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‹œํ‚ค๋Š” ์ผ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‹จ์ˆœ ์ปดํผ๋„ŒํŠธ์—๋งŒ ์‚ฌ์šฉ๋˜์—‡์—ˆ์Œ

ํ•˜์ง€๋งŒ hooks ๋•๋ถ„์— ํ•จ์ˆ˜ ๋ฐฉ์‹์—์„œ๋„ ๋‚ด๋ถ€์ ์œผ๋กœ state๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ณ  lifecycle ์ž‘์—…๋„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Œ

ํ•จ์ˆ˜๊ฐ€ ํด๋ž˜์Šค๋ž‘ ๋Œ€๋“ฑํ•ด์ง„ ๊ฒƒ์ž„

๊ฒŒ๋‹ค๊ฐ€ ํ•จ์ˆ˜์˜ ๊ฐ„ํŽธํ•จ์„ ๊ทธ๋Œ€๋กœ ์ด์–ด๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋” ๊ฐ•๋ ฅํ•ด์ง

ํ•˜์ง€๋งŒ ํด๋ž˜์Šคํ˜•์˜ ์žฅ์ ๋„ ์žˆ์–ด์„œ ๋ฆฌ์•กํŠธ๊ฐ€ ๋งํ•˜๊ธธ ์•ž์œผ๋กœ๋„ ํด๋ž˜์Šคํ˜•์„ ์ง€์›ํ•œ๋‹ค๊ณ  ํ•จ

ํด๋ž˜์Šคํ˜•์„ ํ†ตํ•ด ํ•จ์ˆ˜ํ˜•์„ ๋” ์ž˜ ์ดํ•ดํ•˜๊ณ , ํ•จ์ˆ˜ํ˜•์„ ํ†ตํ•ด ํด๋ž˜์Šคํ˜•์„ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Œ

(+) npx create-react-app .
ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์— ๋ฆฌ์•กํŠธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ ๊ตฌํ˜„

ํ•จ์ˆ˜ํ˜•์€ return ๊ฐ’๋งŒ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ๊ทธ๊ฒŒ ๊ณง ์‹ค์ œ ํ™”๋ฉด์ด ๋œ๋‹ค.
ํด๋ž˜์Šคํ˜•์€ render๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์จ์„œ ๊ทธ return ๊ฐ’์ด ํ™”๋ฉด์ด ๋œ๋‹ค.
์ฆ‰ ํ•จ์ˆ˜ํ˜•์€ ์ž๊ธฐ ์ž์‹ ์ด render์ธ ๊ฒƒ์ด๋‹ค.

import React from 'react'
imoprt './App.css'

function App() {
    return (
        <div className="container">
            <h1>Hello, world!</h1>
            <FuncComp></FuncComp>
            <ClassComp></ClassComp>
        </div>
    );
}

// โ†“ function style component

function FuncComp(){
    return (
        <div className="container">
            <h2>function style component</h2>
        </div>
    );
}

// โ†“ class style component

class ClassComp extends React.Component{
    render(){
        return (
            <div className="container">
                <h2>class style component</h2>
            </div>
        )
    }
}

export default App;

state ์‚ฌ์šฉ๋ฒ•

ํ•œ component์—์„œ state๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ ๊ฐ’์„ ๋‹ค๋ฅธ component๊ฐ€ props๋กœ ๋ฐ›์•„ ์‚ฌ์šฉํ•œ๋‹ค.
props๋Š” ๋‘ ๋ฐฉ์‹ ๋‹ค ์‚ฌ์šฉํ•˜๊ณ , state๋Š” ์›๋ž˜๋Š” ํด๋ž˜์Šคํ˜•์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค๊ฐ€ ์ด์ œ ๋ฐ”๋€ ๊ฒƒ์ž„.
์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

1) ๋ฐ”๋กœ props๋กœ ๋ฐ›์•„์˜ค๊ธฐ (๐Ÿ”ด : ์œ„ ์ฝ”๋“œ์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„. props ๊ฐ’์„ ๊ทธ๋Œ€๋กœ Number ์˜†์— ์ฐ์Œ)
ํด๋ž˜์Šคํ˜•์€ this.props.~, ํ•จ์ˆ˜ํ˜•์€ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ณ  props.~

import React from 'react';
imoprt './App.css';

function App() {
    return (
        <div className="container">
            <h1>Hello, world!</h1>
            <FuncComp ๐Ÿ”ดinitNumber={2}></FuncComp>
            <ClassComp ๐Ÿ”ดinitNumber={2}></ClassComp>
        </div>
    );
}

// โ†“ function style component

function FuncComp(๐Ÿ”ดprops){ // ๋งค๊ฐœ๋ณ€์ˆ˜๋ช…์€ ๋‹ค๋ฅธ ๊ฑธ๋กœ ํ•ด๋„ ๋˜์ง€๋งŒ ๋ณดํ†ต props๋กœ ํ•จ
    return (
        <div className="container">
            <h2>function style component</h2>
            ๐Ÿ”ด<p>Number : {props.initNumber}</p>
        </div>
    );
}

// โ†“ class style component

class ClassComp extends React.Component{
    render(){
        return (
            <div className="container">
                <h2>class style component</h2>
                ๐Ÿ”ด<p>Number : {this.props.initNumber}</p>
            </div>
        )
    }
}

export default App;

2) state๋ฅผ ํ†ตํ•ด ๋ฐ›์•„์˜ค๊ธฐ
ํ•จ์ˆ˜ํ˜•์—์„œ๋Š” ๋ชปํ•จ
(๐Ÿ”ด : ์œ„ ์ฝ”๋“œ์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„. props ๊ฐ’์„ ๊ทธ๋Œ€๋กœ Number ์˜†์— ์ฐ์Œ)

import React from 'react';
imoprt './App.css';

function App() {
    return (
        <div className="container">
            <h1>Hello, world!</h1>
            <FuncComp initNumber={2}></FuncComp>
            <ClassComp initNumber={2}></ClassComp>
        </div>
    );
}

// โ†“ function style component

function FuncComp(props){
    return (
        <div className="container">
            <h2>function style component</h2>
            <p>Number : {props.initNumber}</p>
        </div>
    );
}

// โ†“ class style component

class ClassComp extends React.Component{
  ๐Ÿ”ดstate = {
    number:this.props.initNumber
    }
    render(){
        return (
            <div className="container">
                <h2>class style component</h2>
                <p>Number : {๐Ÿ”ดthis.state.number}</p>
            </div>
        )
    }
}

export default App;

hook

React 16.8๋ถ€ํ„ฐ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์— hook ์‚ฌ์šฉํ•˜๊ธฐ
React์— ๋‚ด์žฅ๋œ Hook์„ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ณ , ๋‚ด๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์จ๋„ ๋œ๋‹ค.
hook์€'use'๋กœ ์‹œ์ž‘๋˜๋Š”๋ฐ, ์ด๋Š” ํŽ˜์ด์Šค๋ถ์ด ์ œ๊ณตํ•˜๋Š” ๋‚ด์žฅ๋œ ํ›…์ด๋‹ค.

1) state, useState


โ†“ ํด๋ž˜์Šคํ˜•

state = {
  number:this.props.initNumber
}

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์— ํ–ˆ๋˜ ์œ„ ์ž‘์—…์„ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์— ๋˜‘๊ฐ™์ด ํ•˜๊ธฐ

import React๐Ÿ”ด, {useState} from 'react';
imoprt './App.css';

function App() {
    return (
        <div className="container">
            <h1>Hello, world!</h1>
            <FuncComp initNumber={2}></FuncComp>
            <ClassComp initNumber={2}></ClassComp>
        </div>
    );
}

// โ†“ function style component

function FuncComp(props){
    ๐Ÿ”ดlet numberState = useState(props.initNumber); // useState๋ฅผ ํ˜ธ์ถœํ•˜๋˜ ๊ทธ ๊ฐ’์„ numberState๋กœ ์„ค์ •
    ๐Ÿ”ดlet number = numberState[0];
    return (
        <div className="container">
            <h2>function style component</h2>
            <p>Number : {props.initNumber}</p>
        </div>
    );
}

// โ†“ class style component

class ClassComp extends React.Component{
  state = {
    number:this.props.initNumber
    }
    render(){
        return (
            <div className="container">
                <h2>class style component</h2>
                <p>Number : {this.state.number}</p>
            </div>
        )
    }
}

export default App;

useState๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌด์กฐ๊ฑด ๋ฐฐ์—ด์ด return๋˜๊ณ  ๊ทธ ๋ฐฐ์—ด์€ ๋‘ ๊ฐœ์˜ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
numberState = useState(props.initNumber);๊ฐ€ returnํ•œ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์ž๋ฆฌ๊ฐ€ state ์—ญํ• ์„ ํ•˜๋Š” ์ดˆ๊ธฐ ๊ฐ’์ด๋‹ค.

์ •๋ฆฌ
ํ•จ์ˆ˜ํ˜•์—์„œ state๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด useState์— ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ ๊ฐ’์ด ์ดˆ๊ธฐ ๊ฐ’์ด ๋œ๋‹ค.

2) setState, useState

ํด๋ž˜์Šคํ˜•์—์„œ๋Š” setState๋กœ state๋ฅผ updateํ•˜์ง€๋งŒ ํ•จ์ˆ˜ํ˜•์—์„œ๋Š” useStat์˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’์ด ๊ทธ ์—ญํ• ์„ ๋Œ€์‹ ํ•œ๋‹ค.


โ†“ ํด๋ž˜์Šคํ˜•

<input type="button" value="random" onClick={
  function(){
    this.setState({number:Math.random()})
  }.bind.(this)
}></input>

โ†“ ํ•จ์ˆ˜ํ˜•

<input type="button" value="random" onClick={
  function(){
    setNumber(Math.random());
  }
}></input>

์ƒํ™œ์ฝ”๋”ฉ

profile
๋Š๋ ค๋„ ํ•  ๊ฑฐ์•ผ.......

0๊ฐœ์˜ ๋Œ“๊ธ€