useEffect
useEffect는 컴포넌트가 렌더링 이후에 특정 작업을 수행하도록 설정 할 수 있습니니다.
자세한 내용은 React 공식 문서에서 확인 가능합니다.
https://ko.reactjs.org/docs/hooks-effect.html
[Using the Effect Hook – React
A JavaScript library for building user interfaces
ko.reactjs.org](https://ko.reactjs.org/docs/hooks-effect.html)
useEffect 사용해보기
Example.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect} from "react"; | |
const Example = () => { | |
const [state, setState] = useState(0); | |
const onChange = () => { | |
setState(state + 1) | |
} | |
useEffect(() => { | |
console.log('+1') | |
}) | |
return ( | |
<> | |
<p>{state}</p> | |
<button onClick={onChange}>+1</button> | |
</> | |
) | |
} | |
export default Example |
App.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import Example from "./componets/Example"; | |
const App = () => { | |
return ( | |
<Example/> | |
) | |
} | |
export default App; |
실행화면

+1 버튼을 누르면 값이 변하고 리렌더링이 됩니다.
useEffect가 리렌더링될 때 마다 실행되는것을 확인할 수 있습니다.
전체 소스코드는 github에서 확인가능합니다.
https://github.com/hanhyung6376/learn-react/tree/master/use-effect
'웹 프로그래밍 > React' 카테고리의 다른 글
[React] useReducer (0) | 2021.10.18 |
---|---|
[React] useRef (0) | 2021.10.16 |
[React] useCallback (0) | 2021.10.13 |
[React] useState (0) | 2021.10.12 |
[React] 외부 라이브러리 없이 캘린더 구현 (0) | 2021.10.11 |