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
- ํ ์ดํ๋ก์ ํธ
- ๊ทธ๋ฆฌ๋
- Python
- HTML
- ๊ตญ๋น์ง์
- ์ฝ๋ฉ์ ํ
- KDT
- react
- fe
- ๋๋ฆผ์ฝ๋ฉ
- Til
- node.js
- CSS
- ์๊ณ ๋ฆฌ์ฆ
- ํ์ด์ฌ
- ํฌ๋กค๋ง
- ํ๋ก์ ํธ
- heapq
- ๋ ธ๋ง๋์ฝ๋
- ๋ชจ๊ฐ์ฝ
- ์ฝ๋ฉํ ์คํธ
- javascript
- ๋ฐฑ์ค
- error
- mongodb
- JS
- ํ๋ก ํธ์๋
- ํ๋ก๊ทธ๋๋จธ์ค
- ๊ฐ๋ฐ
- ์ฝ๋ฉ
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 |