함수 컴포넌트의 useEffect Hook은 class 컴포넌트의 생명주기로 미루어 봤을 때, componentDidMount, componentDidUpdate, componentWillUnmount가 합쳐진 것으로 봐도 좋다.
useEffect Hook을 이용해서 컴포넌트가 렌더링 이후에 어떤 일을 수행해야 하는지 알려준다. 리액트는 우리가 넘긴 함수를 기억해둔다. 여기서 이 함수를 ‘effect’라고 부른다.
useEffect를 컴포넌트 내부에 두는 이유는, effect를 통해 state 변수(또는 그 어떤 prop에도)에 접근할 수 있게 됩니다.
import Router from "next/router";
import { Component, createRef } from "react";
interface IPrev {
count: number;
}
export default class LifecycleClassPage extends Component {
state = {
count: 0,
};
inputRef = createRef<HTMLInputElement>();
componentDidMount() {
this.inputRef.current?.focus();
console.log("안녕!");
}
componentDidUpdate() {
console.log("반가워!");
}
componentWillUnmount() {
alert("잘가!");
}
onClickCount = () => {
this.setState((prev: IPrev) => ({
count: prev.count + 1,
}));
};
onClickMove = () => {
Router.push("/");
};
render() {
return (
<>
<input type="password" ref={this.inputRef} />
<div>카운트 {this.state.count}</div>
<button onClick={this.onClickCount}>더하기 1</button>
<div>이것은 클래스 컴포넌트입니다.</div>
<button onClick={this.onClickMove}>나가기</button>
</>
);
}
}
componentDidMount() 부분에서 기능을 추가하면 된다.
안녕!이 출력되는 것을 확인할 수 있다.
반가워!가 출력되는 것을 확인할 수 있다.
잘가!가 출력되는 것을 확인할 수 있다.
import { useRouter } from "next/router";
import { useEffect, useRef, useState } from "react";
export default function LifecycleFunctionPage() {
const router = useRouter();
const [count, setCount] = useState(0);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
console.log("안녕!");
inputRef.current?.focus();
return () => {
alert("잘가!");
};
}, []);
useEffect(() => {
console.log("반가워!");
}, [count]);
const onClickCount = () => {
setCount((prev) => prev + 1);
};
const onClickMove = () => {
router.push("/");
};
return (
<>
<input type="password" ref={inputRef} />
<div>카운트: {count}</div>
<button onClick={onClickCount}>더하기 1</button>
<div>이것은 함수 컴포넌트 입니다.</div>
<button onClick={onClickMove}>나가기</button>
</>
);
}
useEffect() 부분에서 기능을 추가하면 된다.
안녕!이 출력되는 것을 확인할 수 있다.
반가워!가 출력되는 것을 확인할 수 있다.
잘가!가 출력되는 것을 확인할 수 있다.