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;