강의

멘토링

커뮤니티

Inflearn Community Q&A

heewon97's profile image
heewon97

asked

Learning React while making web games

3-10. shouldComponentUpdate

렌더링 테스트 코드 (Hooks)

Written on

·

73

0

import React, { useState } from "react";

const Test = () => {
    const [counter, setCounter] = useState(0);
   
    const shouldComponentUpdate = (nextProps, nextState, nextContext) => {
        if (counter !== nextState.counter) {
            return true;
        }
        return false;
    }

    const onClick = () => {
        setCounter();
    }   

    console.log('렌더링');
    return (
        <div>
            <button onClick={onClick}>클릭</button>
        </div>
    );
}

export default Test;

클래스로 작성해주셨는데, 제가 Hooks로 변경해서 작성해봤습니다. 클래스와 똑같이 테스트 되는거를 보고싶은데 테스트 결과가 다릅니다.

 

제가 잘 못 짠게 있다면 확인부탁드립니다.!

react

Answer 1

0

zerocho님의 프로필 이미지
zerocho
Instructor

shouldComponentUpdate는 리액트 훅에 대응되는 것이 없습니다! 해당되는 코드는 아예 실행되지도 않을 겁니다. 리액트 라이프사이클은 전부 useEffect로 작성하셔야 합니다(shouldComponentUpdate는 불가능)

heewon97's profile image
heewon97

asked

Ask a question