
- 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;
๋ฐ์ํ
'Frontend > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React | ๋ ๋๋ง ์ฃผ๊ธฐ - ์ ๊ฑฐ ๋จ๊ณ (Unmounting) (0) | 2023.12.04 |
---|---|
React | ๋ ๋๋ง ์ฃผ๊ธฐ - ์ ๋ฐ์ดํธ ๋จ๊ณ (Updating) (0) | 2023.12.01 |
React | ๋ ๋๋ง ์ฃผ๊ธฐ (2) | 2023.11.29 |
React | ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋(Lifecyle Methods) (0) | 2023.11.28 |
React | ๊ฐ์ฒด ํ์ State ์ ๋ฐ์ดํธ (0) | 2023.11.27 |