Frontend/React
React | ๋ ๋๋ง ์ฃผ๊ธฐ - ์์ฑ ๋จ๊ณ (Mounting)
์ด์ ํฌ
2023. 11. 30. 17:00
- counstructor()
- ์ปดํฌ๋ํธ ์์ฑ ์ ๊ฐ์ฅ ๋จผ์ ํธ์ถ๋๋ ์ฒซ๋ฒ์งธ ๋ฉ์๋
- ์ด๊ธฐ ์ํ ์ค์ ๋ฐ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ์ํํ๋ค.
- state getDerivedStateFromProps()
- props๋ก๋ถํฐ ์ํ๋ฅผ ๋๊ธฐํ ํ๊ณ ์ ํ ๋ ํธ์ถํ๋ ๋ฉ์๋
- render()
- ์ปดํฌ๋ํธ UI ๋ ๋๋ง์ ์ํํ๋ค.
- componentDidMount()
- ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋ ์ค ํ๋.
- ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง์ด ์๋ฃ ๋๊ณ DOM์ ๋ง์ดํธ ๋๊ณ ๋ ์ดํ์ ์คํ๋๋ ๋ฉ์๋
- ์ด๊ธฐ ๋ฐ์ดํฐ ๋ก๋ฉ ํน์ ์ธ๋ถ ๋ฐ์ดํฐ ์์ฒญ ๋ฑ์ ๋น๋๊ธฐ ์์ ์ ์คํํ๋ค.
import React, { Component } from 'React'
class MyComponent extends Component {
constructor(props) {
super(props);
// ์ด๊ธฐ ์ํ ์ค์
this.state = {
count : 0,
};
}
static getDerivedStateFromProps(nextProps, prevState) {
// nextProps๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ํ๋ฅผ ์
๋ฐ์ดํธ
if (nextProps.someValue !== prevState.someValue) {
return {
someValue: nextProps.someValue,
};
}
// ์ํ๋ฅผ ์
๋ฐ์ดํธํ ํ์๊ฐ ์๋ค๋ฉด null์ ๋ฐํ
return null;
}
render() {
return (
<div>
<p>{this.state.count}</p>
</div>
);
}
componentDidMount() {
// ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ๋ ํ์ ์คํ
console.log("Component is mounted!");
}
}
export default MyComponent;
๋ฐ์ํ