๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

[React] 7. State and Lifecycle

category Web/React 2023. 11. 5. 16:40

State

  • ๋ฆฌ์•กํŠธ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ.
  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ.
  • state๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ Œ๋”๋ง์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’๋งŒ state์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•œ๋‹ค.
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด

Lifecycle

  • ์ƒ๋ช…์ฃผ๊ธฐ
  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ 
  1. ์ƒ์„ฑ์ž constructor
  2. ๋ Œ๋”๋ง render
  3. ์–ธ๋งˆ์šดํŠธ 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