์๋ช ์ฃผ๊ธฐ ๋ฉ์๋
์ปดํฌ๋ํธ๊ฐ ๋ธ๋ผ์ฐ์ ์์ ๋ํ๋๊ณ , ์ ๋ฐ์ดํธ๋๊ณ , ์ฌ๋ผ์ง๊ฒ ๋ ๋ ํธ์ถ๋๋ ๋ฉ์๋๋ค (+ ์๋ฌ๋ฐ์ ์ ํธ์ถ๋๋ ๋ฉ์๋๋ ์กด์ฌ)
- ํด๋์คํ ์ปดํฌ๋ํธ์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
React Lifecycle Methods diagram
React Lifecycle Methods diagram
Fully interactive and accessible React Lifecycle Methods diagram.
projects.wojtekmaj.pl

๋ง์ดํธ๋ ๋ ๋ฐ์ํ๋ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋
1. constructor
- ์ปดํฌ๋ํธ์ ์์ฑ์ ๋ฉ์๋
- ์ปดํฌ๋ํธ๊ฐ ๋ง๋ค์ด์ง๋ฉด ๊ฐ์ฅ ๋จผ์ ์คํ๋๋ ๋ฉ์๋
constructor(props){
super(props);
console.log("constructor");
}
2. getDerivedStateFromProps
- props๋ก ๋ฐ์์จ ๊ฒ์ state์ ๋ฃ์ด์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉ
- ์์ static ํ์, ์์ this๋ฅผ ์กฐํ ํ ์ ์์
- ํน์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ฒ ๋๋ฉด ํด๋น ๊ฐ์ฒด ์์ ์๋ ๋ด์ฉ๋ค์ด ์ปดํฌ๋ํธ์ state๋ก ์ค์ ๋จ, null์ ๋ฐํํ๊ฒ ๋๋ฉด ์๋ฌด ์ผ๋ ๋ฐ์ ์ํจ
- ์ด ๋ฉ์๋๋ ์ฒ์ ๋ ๋๋ง ๋๊ธฐ ์ ์๋ ํธ์ถ๋๊ณ , ๊ทธ ์ดํ ๋ ๋๋ง ๋๊ธฐ ์ ์๋ ๋งค๋ฒ ์คํ
static getDerivedStateFromProps(nextProps, preState){
console.log("getDerivedStateFromProps");
if (nextProps.color !== preState.color){
return {color: nextProps.color };
}
return null;
}
3. render
- ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ ๋ฉ์๋
4. componentDidMount
- ์ปดํฌ๋ํธ์ ์ฒซ ๋ฒ์งธ ๋ ๋๋ง์ด ๋ง์น๊ณ ๋๋ฉด ํธ์ถ๋๋ ๋ฉ์๋
- ์ด ๋ฉ์๋๊ฐ ํธ์ถ๋๋ ์์ ์๋ ๋ง๋ ์ปดํฌ๋ํธ๊ฐ ํ๋ฉด์ ๋ํ๋ ์ํ์
- DOM์ ์ฌ์ฉํด์ผํ๋ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฐ๋ํ๊ฑฐ๋, ํด๋น ์ปดํฌ๋ํธ์์ ํ์๋ก ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ํ๊ธฐ ์ํด axios ๋ฑ ์ด์ฉ
์ ๋ฐ์ดํธ ๋ ๋ ํธ์ถ๋๋ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋
1. getDerivedStateFromProps
- ์ฒ์ ๋ ๋๋ง ๋๊ธฐ ์ ์๋ ํธ์ถ๋๊ณ , ์ปดํฌ๋ํธ์ props๋ state๊ฐ ๋ฐ๋์์ ๋๋ ํธ์ถ
2. shouldComponentUpdate
- ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง ํ ์ง ๋ง์ง ๊ฒฐ์ ํ๋ ๋ฉ์๋
- ์ฃผ๋ก ์ต์ ํ ํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋
shouldComponentUpdate(nextProps, nextState){
console.log("shouldComponentUpdate", nextProps, nextState);
//์ซ์์ ๋ง์ง๋ง ์๋ฆฌ๊ฐ 4๋ฉด ๋ฆฌ๋ ๋๋งํ์ง ์์
return nextState.number % 10 !== 4;
}
3. render
- ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ ๋ฉ์๋
4. getSnapshotBeforeUpdate
- ์ปดํฌ๋ํธ์ ๋ณํ๊ฐ ์ผ์ด๋๊ธฐ ์ง์ ์ DOM ์ํ๋ฅผ ๊ฐ์ ธ์์ ํน์ ๊ฐ์ ๋ฐํ
getSnapshotBeforeUpdate(prevProps, prevState){
console.log("getSnapshotBeforeUpdate");
if(prevProps.color !== this.props.color){
return this.myRef.style.color;
}
return null;
}
5. componentDidUpdate
- ๋ฆฌ๋ ๋๋ง์ด ๋ง์น๊ณ , ํ๋ฉด์ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋ณํ๊ฐ ๋ชจ๋ ๋ฐ์๋๊ณ ๋ ๋ค ํธ์ถ๋๋ ๋ฉ์๋
- DOM์ ๋ณํ๊ฐ ๋ฐ์๋๊ธฐ ์ง์ ์ DOM์ ์์ฑ์ ํ์ธํ๊ณ ์ถ์ ๋ ์ฌ์ฉ
componentDidUpdate(prevProps, prevState, snapshot){
console.log("componentDidUpdate", prevProps, prevState);
if(snapshot){
console.log("์
๋ฐ์ดํธ ๋๊ธฐ ์ง์ ์์: ",snapshot);
}
}
์ธ๋ง์ดํธ๋ ๋ ํธ์ถ๋๋ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋
1. componentWillUnmout
- ์ปดํฌ๋ํธ๊ฐ ํ๋ฉด์์ ์ฌ๋ผ์ง๊ธฐ ์ง์ ์ ํธ์ถ
componentWillUnmount(){
console.log("componentWillUnmout");
}
- DOM์ ์ง์ ๋ฑ๋กํ์๋ ์ด๋ฒคํธ๋ฅผ ์ ๊ฑฐํ๊ณ , ๋ง์ฝ์ setTimeout์ ๊ฑธ์ ๊ฒ์ด ์์ผ๋ฉด clearTimeout์ ํตํด ์ ๊ฑฐ
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ dispose๊ธฐ๋ฅ์ด ์์ผ๋ฉด componentWillUnmount์์ ํธ์ถ
์๋ฌ ๋ฐ์์ ํธ์ถ๋๋ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋, componentDidCatch
- ๋ฆฌ์กํธ ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ฐ์ํ๋ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ
๋ฆฌ์กํธ ์ฑ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ์ํฉ
1. props๋ฅผ ์ ๋๋ก ์ค์ ํ์ง ์์ ๊ฒฝ์ฐ
import React from "react";
function User({ user }) {
return (
<div>
<div>
<b>ID</b>: {user.id}
</div>
<div>
<b>Username:</b> {user.username}
</div>
</div>
);
}
export default User;
import React from "react";
import User from "./User";
function App() {
const user = {
id: 1,
useranme: "velopert",
};
return <User />;
}
export default App;
[ํด๊ฒฐ ๋ฐฉ๋ฒ]: ๊ฐ์ด ์กด์ฌํ์ง ์๋๋ค๋ฉด null์ ๋ ๋๋ง ํ๊ฒ ํ๊ธฐ(null checking)
import React from "react";
function User({ user }) {
if (!user) {
return null;
}
return (
<div>
<div>
<b>ID</b>: {user.id}
</div>
<div>
<b>Username:</b> {user.username}
</div>
</div>
);
}
export default User;
2. ๊ฐ ์ค์ ์ ํด์ฃผ์ง ์์ ๊ฒฝ์ฐ
function Users({users}){
return(
<ul>
{users.map(user => (
<li key={user.id}>{user.username}</li>
))}
);
}
[ํด๊ฒฐ ๋ฐฉ๋ฒ]: users ๊ฐ ์์ผ๋ฉด ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฐํํ๋ ์์
function Users({ users }) {
if (!users) return null;
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.username}</li>
))}
</ul>
);
}
3. props ์ ๋ฌํ์ง ์์ ๊ฒฝ์ฐ
function Users({ users, onToggle }) {
if (!users) return null;
return (
<ul>
{users.map(user => (
<li key={user.id} onClick={() => onToggle(user.id)}>
{user.username}
</li>
))}
</ul>
);
}
[ํด๊ฒฐ ๋ฐฉ๋ฒ]: props๋ก ๋ฃ์ด์ฃผ๋ ๊ฒ์ ๊น๋จน์ง ์๊ธฐ ์ํด defaultProps ์ค์
Users.defaultProps = {
onToggle: () => {
console.warn('onToggle is missing!');
}
};
componentDidCatch๋ก ์๋ฌ ์ก์๋ด๊ธฐ
: ์ฌ์ ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํ์ง ์์ ์๋ฌ๊ฐ ๋ฐ์ ํ์ ๋ ์ฌ์ฉ์์๊ฒ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค๊ณ ์๋ ค์ฃผ๋ ํ๋ฉด์ ๋ณด์ฌ์ค
import React, { Component } from "react";
class ErrorBoundary extends Component {
state = {
error: false,
};
componentDidCatch(error, info) {
console.log("์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
console.log({
error,
info,
});
this.setState({
error: true,
});
}
render() {
if (this.state.error) {
return <h1>์๋ฌ ๋ฐ์!</h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;
- componentDidCatch ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ์๋ฌ์ ๋ด์ฉ, ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ์๋ฌ๊ฐ ๋ฐ์ํ ์์น
- this.state.error๊ฐ์ด true์ด๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค๋ ๋ฌธ๊ตฌ ๋ ๋๋ง, ๊ทธ๋ ์ง ์์ผ๋ฉด this.props.children์ ๋ ๋๋งํ๋๋ก ์ฒ๋ฆฌ
- ๊ทธ ํ, App ์ปดํฌ๋ํธ์์ <User />์ปดํฌ๋ํธ ๊ฐ์ธ์ค
return(
<ErrorBoundary>
<User />
</ErrorBoundary>
);
Sentry ์ฐ๋
: ๊ฐ๋ฐ์๊ฐ ๋ฐ๊ฒฌํด๋ด์ง ๋ชปํ์ง๋ง, ์ฌ์ฉ์๊ฐ ๋ฐ๊ฒฌํ๊ฒ ๋๋ ์ค๋ฅ๊ฐ ์์ ์์, componentDidCatch์์ error์ info๊ฐ์ ๋คํธ์ํฌ๋ฅผ ํตํด ๋ค๋ฅธ ๊ณณ์ผ๋ก ์ ๋ฌ ํด์ค → Sentry ์์ฉ์๋น์ค
Sign In | Sentry
sentry.io
'ํ๋ก ํธ์๋ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Sass (0) | 2022.12.06 |
|---|---|
| ๋ฆฌ์กํธ ๊ฐ๋ฐ ์ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํ ๋๊ตฌ (0) | 2022.12.02 |
| ํด๋์คํ ์ปดํฌ๋ํธ (0) | 2022.12.02 |
| Immer (0) | 2022.11.24 |
| Context API (0) | 2022.11.24 |