Grace
grace's dev_note
Grace
전체 방문자
오늘
어제
  • 분류 전체보기
    • FrontEnd
      • Next.js
      • React
      • ReactNativ..
      • Vue
    • Javascript
      • 러닝 자바스크립트
      • 모던 자바스크립트
    • CS
    • DataScienc..
      • Data Struc..
      • LeetCode
    • BackEnd
      • Express
      • Node.js
      • Nest.js
    • DevOps
      • Docker
    • 매일메일
    • 회고
    • 코드캠프

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 자바스크립트
  • nest.js
  • Vue
  • vue-query
  • postgres
  • node.js
  • Vite
  • 함수
  • backend
  • Vue.js
  • vitejs
  • javascript
  • PostgreSQL
  • tanstack
  • React Native
  • Express
  • 번들러
  • pinia
  • 알고리즘
  • Vue3

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Grace

grace's dev_note

코드캠프

코드캠프 16일차

2022. 10. 2. 20:50

컴포넌트 생명주기

render

그리기

componentDidMount

그리고 난 뒤

componentDidUpdate

그리고 난 후 변경됐을 때

componentDidUnmount

그리고 난 뒤 사라질 때

클래스 컴포넌트

컴포넌트를 만드는 설명서
클래스 안에 있는 함수들은 메서드라고 부른다.

component, createRef, Router 를 사용하기 위해 import

import { Component, createRef } from "react";
import Router from "next/router";

타입스크립트이므로 count 에 number를 설정

interface IState {
  count: number;
}

Component 클래스형 컴포넌트라 옛방식들이다.

export default class ClassComponentLifecyclePage extends Component {
  inputRef = createRef<HTMLInputElement>();
  state = {
    count: 0,
  };
componentDidMount = () => {
    console.log("컴포넌트 마운트 완료!!");
    this.inputRef.current.focus(); 
  };
componentDidUpdate = () => {
    console.log("컴포넌트 수정 완료!!");
  };
componentWillUnmount = () => {
    console.log("잘가요~~");
  };
onClickCount = () => {
    this.setState((prev: IState) => ({
      count: prev.count + 1,
    }));
  };
onClickMove = () => {
    Router.push("/");
  };
render() {
    return (
      <>
        현재카운트 : {this.state.count}
        <button onClick={this.onClickCount}>카운트 증가!!!</button>
        <button onClick={this.onClickMove}> 페이지 이동하기 </button>
        <input type="text" ref={this.inputRef} />
      </>
    );
  }
}

this

클래스나 객체에 포함되지 않으면 기본적으로 window를 지칭한다.

클래스 연산자에서 함수(메서드)를 연결해줄 경우 onClick={this.onClickCounter.bind(this)} 이런 식으로 bind를 걸어주어야한다.

함수형 컴포넌트

useEffect, useRef, useState, useRouter를 쓰기위해 import

import { useEffect, useRef, useState } from "react";
import { useRouter } from "next/router";

const

export default function FunctioanlComponentLifecyclePage() {
  const router = useRouter();
  const [count, setCount] = useState(0);
  const inputRef = useRef<HTMLInputElement>();

componentDidMount 와 동일

useEffect(() => {
  console.log("컴포넌트 마운트 완료!!");
  inputRef.current.focus();
componentWillUnmount 와 동일
    return () => {
      console.log("잘가요~~");
    };
  }, []);

componentDidUpdate 와 동일

  useEffect(() => {
    console.log("컴포넌트 수정 완료!!");
  });

####함수선언 및 return

   function onClickCount() {
    setCount((prev) => prev + 1);
  }
  function onClickMove() {
    router.push("/");
  }
  return (
    <>
      현재카운트 : {count}
      <button onClick={onClickCount}>카운트증가!!</button>
      <button onClick={onClickMove}>페이지이동</button>
      <input type="text" ref={inputRef} />
    </>
  );
 }
저작자표시 비영리 변경금지 (새창열림)

'코드캠프' 카테고리의 다른 글

코드캠프 19일차  (0) 2022.10.02
코드캠프 17일차  (0) 2022.10.02
코드캠프 15일차  (0) 2022.10.02
코드캠프 14일차  (0) 2022.10.02
코드캠프 13일차  (0) 2022.10.02
    '코드캠프' 카테고리의 다른 글
    • 코드캠프 19일차
    • 코드캠프 17일차
    • 코드캠프 15일차
    • 코드캠프 14일차
    Grace
    Grace
    기술 및 회고 블로그

    티스토리툴바