State
- ๋ฆฌ์กํธ์ปดํฌ๋ํธ์ ์ํ.
- ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๋ฐ์ดํฐ.
- state๋ ๊ฐ๋ฐ์๊ฐ ์ ์ํ์ฌ ์ฌ์ฉํ๋ค.
- ๋ ๋๋ง์ด๋ ๋ฐ์ดํฐ ํ๋ฆ์ ์ฌ์ฉ๋๋ ๊ฐ๋ง state์ ํฌํจ์์ผ์ผ ํ๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด
Lifecycle
- ์๋ช ์ฃผ๊ธฐ
- ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ ์๋ช ์ฃผ๊ธฐ
- ์์ฑ์ constructor
- ๋ ๋๋ง render
- ์ธ๋ง์ดํธ componentWillUnmount ์์์ปดํฌ๋ํธ์์ ํ์ฌ์ปดํฌ๋ํธ๋ฅผ ๋์ด์ ํ๋ฉด์ ํ์๋์ง ์๊ฒ ๋ ๋ componentWillUnmount๊ฐ ํธ์ถ๋จ
์ปดํฌ๋ํธ๊ฐ ๊ณ์ ์กด์ฌํ๋ ๊ฒ์ด ์๋๋ผ ์๊ฐ์ ํ๋ฆ์ ๋ฐ๋ผ ์์ฑ๋๊ณ ์ ๋ฐ์ดํธ๋๋ค๊ฐ ์ฌ๋ผ์ง๋ค.
์๋ฆผ ์ค์ต
- React Developer Tools ( React Developer Tools ) ํฌ๋กฌ ํ์ฅ ํ๋ก๊ทธ๋จ ์ค์น ํ ์ฌ์ฉ
- ์ฝ์ ๋์ ์ด๊ฒ์ ์ฌ์ฉ
Notification.jsx
import React from "react";
const styles = {
wrapper: {
margin: 8,
padding: 8,
display: "flex",
flexDirection: "row",
border: "1px solid grey",
borderRadius: 16,
},
messageText: {
color: "black",
fontSize: 16,
},
};
class Notification extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidMount() {
console.log(`${this.props.id} componentDidMount() called.`);
}
componentDidUpdate() {
console.log(`${this.props.id} componentDidUpdate() called.`);
}
componentWillUnmount() {
console.log(`${this.props.id} componentWillUnmount() called.`);
}
render() {
return (
<div style={styles.wrapper}>
<span style={styles.messageText}>{this.props.message}</span>
</div>
);
}
}
export default Notification;
NotificationList.jsx
import React from "react";
import Notification from "./Notification";
const reservedNotifications = [
{
id: 1,
message: "์๋
ํ์ธ์ ์๋
ํ์ธ์~",
},
{
id: 2,
message: "๋ฐ๊ฐ์ต๋๋ค ๋ฐ๊ฐ์์~",
},
{
id: 3,
message: "๋ฌด์กฐ๊ฑด ์ ๋ ๊ฒ๋๋ค~",
},
];
var timer;
class NotificationList extends React.Component {
constructor(props) {
super(props);
this.state = {
notifications: [],
};
}
componentDidMount() {
const {notifications} = this.state;
timer = setInterval(() => {
if (notifications.length < reservedNotifications.length) {
const index = notifications.length;
// js์ list๋ push๋ก ๊ฐ์ ๋ฃ์ด์ค๋ค.
notifications.push(reservedNotifications[index]);
this.setState({
// ํด๋์ค ์ปดํฌ๋ํธ์์ state๋ฅผ ์
๋ฐ์ดํธํ๋ ค๋ฉด ๋ฐ๋์ setState๋ฅผ ์ฌ์ฉํ๋ค.
notifications: notifications,
});
} else {
// notifications ๋น์ฐ๊ธฐ
this.setState({
notifications: [],
});
clearInterval(timer);
}
}, 1000);
}
render() {
return (
<div>
{this.state.notifications.map((noti) => {
return <Notification key={noti.id} id={noti.id} message={noti.message} />;
})}
</div>
);
}
}
export default NotificationList;
index.js
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
//import App from "./App";
import reportWebVitals from "./reportWebVitals";
//import Library from "./chapter_03/Library";
// import Clock from "./chapter_04/Clock";
// import CommentList from "./chapter_05/CommentList";
import NotificationList from "./chapter_06/NotificationList";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<NotificationList />
</React.StrictMode>
);
reportWebVitals();
๊ฒฐ๊ณผ
'Web > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] 8. Hooks (0) | 2023.11.06 |
---|---|
[React] 6. Components and Props (0) | 2023.11.05 |
[React] Error: react_dom_client__WEBPACK_IMPORTED_MODULE_1__.render is not a function (0) | 2023.11.05 |
[React] 6. Rendering Elements (0) | 2023.11.05 |
[React] Error: Already included file name (0) | 2023.11.05 |