Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- ๋ฐฑ์ค
- CSS
- ๊ตญ๋น์ง์
- heapq
- ํ๋ก ํธ์๋
- JS
- javascript
- ์ฝ๋ฉ์ ํ
- ๊ทธ๋ฆฌ๋
- ํ๋ก์ ํธ
- ๋ชจ๊ฐ์ฝ
- ๋๋ฆผ์ฝ๋ฉ
- mongodb
- ์ฝ๋ฉํ ์คํธ
- KDT
- ์ฝ๋ฉ
- ํ์ด์ฌ
- Til
- Python
- ๋ ธ๋ง๋์ฝ๋
- ํฌ๋กค๋ง
- fe
- HTML
- ํ๋ก๊ทธ๋๋จธ์ค
- error
- ๊ฐ๋ฐ
- ํ ์ดํ๋ก์ ํธ
- node.js
- ์๊ณ ๋ฆฌ์ฆ
- react
Archives
- Today
- Total
๐ฑ → ๐ณ
[React] useState vs useRef vs Variable ๋ณธ๋ฌธ
728x90
useState vs useRef vs Variable
useState ์ state ๊ทธ๋ฆฌ๊ณ useRef ์ ref ์ ๋ฆฌ์กํธ ๋ด๋ถ์ ๋ณ์๊ฐ ๋ ๋๋ง์ ๋ฐ๋ผ ์ด๋ค ์์ผ๋ก ๋ณํํ ๊น
์ธ ๊ฐ์ง ๋ฒํผ์ผ๋ก ๊ฐ๊ฐ state/ ref / var ๊ฐ์ ์ฌ๋ฆฌ๋ ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ฑ
- ๊ฐ์ ๋ณํ ์์ด ์ปดํฌ๋ํธ ๋ฆฌ๋ ๋๋ง์ ์ํด์ ํ๋์ ๋ฒํผ์ ๋ ๋ง๋ฆ
→ ๊ฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด์ ํด๋น ๊ฐ์ ๋ณํ ๊ด์ฐฐ
import { useRef, useState } from "react";
const Comparing = () => {
const [countState, setState] = useState(0);
const [render, setRender] = useState(0);
const countRef = useRef(0);
let countVar = 0;
const countUpState = () => {
setState(countState + 1);
console.log("State: ", countState);
};
const countUpRef = () => {
countRef.current = countRef.current + 1;
console.log("Ref: ", countRef.current);
};
const countUpVar = () => {
countVar = countVar + 1;
console.log("Variable: ", countVar);
};
const reRender = () => {
setRender(render + 1);
};
return (
<>
<h1>State: {countState}</h1>
<h1>Ref: {countRef.current}</h1>
<h1>Variable: {countVar}</h1>
<button onClick={countUpState}>State UP!</button>
<button onClick={countUpRef}>Ref UP!</button>
<button onClick={countUpVar}>Variable UP!</button>
<button onClick={reRender}>๋ ๋๋ง!</button>
</>
);
};
export default Comparing;
728x90
'Client > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] React.Fragment (0) | 2022.09.30 |
---|---|
[React] JSX (Javascript XML) (1) | 2022.09.26 |