๐Ÿ“–[React]State & LifeCycle

ํ˜ฑยท2022๋…„ 7์›” 7์ผ

React

๋ชฉ๋ก ๋ณด๊ธฐ
7/28

๐Ÿ’— State?

โœ ๋ฆฌ์•กํŠธ Component์˜ ์ƒํƒœ, ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ

state๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•˜๋ฉฐ
๋ Œ๋”๋ง์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’๋งŒ state์— ํฌํ•จ์‹œ์ผœ์•ผํ•จ

state๋Š” JacaScript์˜ ๊ฐ์ฒด์ด๋‹ค.

class LikeBtn extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      liked: flase
    };
  } 
}

state๋Š” ์ง์ ‘ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ setState ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๋ณ€๊ฒฝํ•ด์•ผํ•จ

๐Ÿ’— LifeCycle

Class ์ปดํฌ๋„ŒํŠธ ๊ฐœ๋…์—์„œ ๊ธฐ์–ตํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ด๋Ÿฐ ๊ฒƒ์ด ์žˆ๋‹ค+์ปดํฌ๋„ŒํŠธ ๋งˆ์ง€๋ง‰ ํŠน์ง•๋งŒ ์ƒ๊ฐํ•ด๋‘๊ธฐ

โœ ๋ฆฌ์•กํŠธ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ

์ถœ์ƒ: Mounting, ์ปดํฌ๋„ŒํŠธ state ํ˜ธ์ถœ๋จ

์ธ์ƒ: Updating

์‚ฌ๋ง: Unmounting, ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋”์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒŒ ๋  ๋•Œ

Component๊ฐ€ ๊ณ„์† ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์‹œ๊ฐ„์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ์ƒ์„ฑ๋˜๊ณ  ์—…๋ฐ์ดํŠธ ๋˜๋‹ค๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค

๐Ÿ’— ์ฝ”๋“œ ์˜ˆ์‹œ

const styles = {
  wrapper: {
    margin : 8,
    padding: 8,
    display: "flex",
    flexDirection: "row",
    border: "1px solid gray",
    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."`) };
  componentDidMount(){
    console.log(`${this.props.id} "componentDidUpMount() 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;

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: [], //state์—์„œ ์ดˆ๊ธฐํ™”
    };
  }

  componentDidMount() {
    const {notifications} = this.state;
    timer = setInterval(() => { //1์ดˆ๋งˆ๋‹ค ์ •ํ•ด์ง„ ๋ฐฐ์—ด ์ˆ˜ํ–‰
      if(notifications.length<reservedNotifications.length){
        const index = notifications.length;
        notifications.push(reservedNotifications[index]);
        this.setState({ //state ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์œ„ํ•ด์„œ
          notifications: notifications,
        });
      }else{
        this.setState({
          notifications: [],
        });
        clearInterval(timer);
      }
    }, 1000);
  }

  render() {
    return(
      <div>
        {this.state.notifications.map((notification) => {
          return (
          <Notification 
          key={notification.id}
          id={notification.id}
          message={notification.message}
          />
          );
          })}
      </div>
    );
  }
}
profile
new blog: https://hae0-02ni.tistory.com/

0๊ฐœ์˜ ๋Œ“๊ธ€