Javascript/React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ

[React + Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ] (3) ๋ฆฌ์•กํŠธ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ, ๋ผ์ดํ”„ ์‚ฌ์ดํด

Rainbow๐ŸŒˆCoder 2022. 12. 5. 18:50
728x90

0. ํ•œ๋ˆˆ์— ๋น„๊ตํ•˜๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ vs ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

์ขŒ(ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ) ์šฐ(ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ)

 

1. ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ shortCut : rcc

(ํ•จ์ˆ˜ ์ปดํผ๋„ŒํŠธ ์ƒ์„ฑ์€ : rafce : react arrow function component export)

import React, { Component } from 'react'

export default class AppClass extends Component {
  render() {
	return (
	  <div>AppClass</div>
	)
  }
}

function์ด class๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

AppClass.js

: ํด๋ฆญ์ˆ˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ฒ„ํŠผ ๊ทธ๋ฆฌ๋Š” ์˜ˆ์ œ

import React, { Component } from 'react';
import './App.css';

export default class AppClass extends Component {
	constructor(props) {
		super(props);
		//state๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— ์›ํ•˜๋Š” state๋ฅผ ๋‹ค ๋„ฃ์œผ๋ฉด ๋จ
		this.state = {
			counter: 0,
		}
	}
	increase = () => {
		this.setState({
			counter: this.state.counter + 1,
		});
	}
	render() {
		return (
			<div>
				<button onClick={this.increase}>{this.state.counter}</button>
			</div>
		);
	}
}

 

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import AppClass from './AppClass';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
	<React.StrictMode>
		<AppClass />
	</React.StrictMode>
);


reportWebVitals();

 

2. ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ lifecycle 

๋ณผ๋“œ ๊ธ€์”จ์ฒ˜๋ฆฌ ๋œ ๊ฒƒ๋“ค์ด ์ค‘์š”!

 

 

 

constructor

 constructor(์ƒ์„ฑ์ž)์ด๋‹ค. ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์ดˆ๊ธฐ state๋ฅผ ์ •ํ•œ๋‹ค.

ํ›…์—์„œ๋Š” useState hook์„ ์‚ฌ์šฉํ•˜๋ฉด ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์‰ฝ๊ฒŒ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

// Class
class Example extends React.Component {
    constructor(props) {
        super(props);
        this.state = { count: 0 };
}

// Hooks
const Example = () => {
    const [count,setCount] = useState(0);
}

 

render

UI ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜.

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” render๋ฅผ ์•ˆ์“ฐ๊ณ  ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

// Class
class Example extends React.Component {
  render() {
    return <div>์ปดํฌ๋„ŒํŠธ</div>
  }
}

// Hooks
const example = () => {
  return <div>์ปดํฌ๋„ŒํŠธ</div>
}

 

 

componentDidMount

//Api call

UI ์„ธํŒ…์ด ์™„๋ฃŒ๋˜๋ฉด ์•Œ๋ ค์คŒ. UI is Ready

ํ•จ์ˆ˜ํ˜• Hooks ์—์„œ๋Š” useEffect๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์Œ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

// Class
class Example extends React.Component {
    componentDidMount() {
        ...
    }
}

// Hooks
const Example = () => {
    useEffect(() => {
        ...
    }, []);
}

 

์—ฌ๊ธฐ์„œ useEffect์˜ [] ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ๋น„์›Œ์•ผ์ง€๋งŒ ๋˜‘๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 ๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ-useEffect

 

ComponentDidUpdate

 

๋”ฐ๋ผ์„œ ์ตœ์‹  ์—…๋ฐ์ดํŠธ๋œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ์ž‘์—…์„ ํ•ด์•ผํ•  ๋•Œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์—…๋ฐ์ดํŠธ๊ฐ€ ๋๋‚œ ์งํ›„์ด๋ฏ€๋กœ, DOM๊ด€๋ จ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. 

// Class
class Example extends React.Component {
    componentDidUpdate(prevProps, prevState) {
        ...
    }
}

// Hooks
const Example = () => {
    useEffect(() => {
        ...
    });
}

 

componentWillUnmount

์ด ๋ฉ”์„œ๋“œ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ DOM์—์„œ ์ œ๊ฑฐํ•  ๋•Œ ์‹คํ–‰ํ•œ๋‹ค. componentDidMount์—์„œ ๋“ฑ๋กํ•œ ์ด๋ฒคํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์ œ๊ฑฐ ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค.

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useEffect CleanUp ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

// Class
class Example extends React.Component {
    coomponentWillUnmount() {
        ...
    }
}

// Hooks
const Example = () => {
    useEffect(() => {
        return () => {
            ...
        }
    }, []);
}

 

3. ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” reack hook์ธ useEffect ์ด์šฉ

(1) ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ componentDidMount์˜ ๊ธฐ๋Šฅ์„ ์ปค๋ฒ„

* ๋‘๋ฒˆ์งธ ์ธ์ž ๋ฐฐ์—ด์— ์•„๋ฌด ๊ฐ’๋„ ์—†์–ด์•ผ componentDidMount ์ฒ˜๋Ÿผ ์ž‘๋™

useEffect๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ 2๊ฐœ ๋ฐ›๋Š”๋‹ค.

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ componentDidMount์˜ ๊ธฐ๋Šฅ์„ ์ปค๋ฒ„ํ•ด์คŒ

๋”ฐ๋ผ์„œ api ํ˜ธ์ถœ ์ž‘์—…์„ useEffect์—์„œ ํ•จ

	useEffect(() => {
		
	}, []);

(2) ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ componentDidUpdate์˜ ๊ธฐ๋Šฅ์„ ์ปค๋ฒ„

* ๋‘๋ฒˆ์งธ ์ธ์ž ๋ฐฐ์—ด์— ์–ด๋– ํ•œ ๊ฐ’์ด๋ผ๋„ ์žˆ์–ด์•ผ componentDidUpdate ์ฒ˜๋Ÿผ ์ž‘๋™

๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋ ๋•Œ๋งˆ๋‹ค ์‹คํ–‰

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

ํ•ด๋‹น ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋˜๋ฉด ์•Œ๋ ค์ฃผ๊ฒŒ(์ฒซ๋ฒˆ์งธ ์ธ์ž์ธ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์‹คํ–‰) ๋œ๋‹ค.

 

	useEffect(() => {
	}, [counter]);

 

import { useState, useEffect } from 'react';
import './App.css';

function App() {
	const [counter, setcounter] = useState(0);
	const increase = () => {
		setcounter(counter + 1);
	}

	useEffect(() => {
	}, [counter]);
	return (
		//html๋‹จ
		<div>
			<button onClick={increase}>
				{counter}
			</button>
		</div>
	);
}

export default App;

 

์ฐธ๊ณ  ๋ฌธํ—Œ : https://kyun2da.dev/react/%EB%A6%AC%EC%95%A1%ED%8A%B8-%EB%9D%BC%EC%9D%B4%ED%94%84%EC%82%AC%EC%9D%B4%ED%81%B4%EC%9D%98-%EC%9D%B4%ED%95%B4/

728x90