[TIL/React] 2023/06/24
useRef ๐ข Reference ๐ก ์ปดํฌ๋ํธ์ ์ต์๋จ์์ useRef๋ฅผ ํธ์ถํ์ฌ ref๋ฅผ ์ ์ธํ๋ค. Usage ๐ก Referencing a value with a ref ๐ฃ ref๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ฐธ์กฐํ ์ ์๋ค. ์ ์ปดํฌ๋ํธ๋ ๋ฒํผ์ด ํด๋ฆญ๋ ํ์๋ฅผ
useRef ๐ข
useRef๋ ``rendering์ ํ์ํ์ง ์์ ๊ฐ`์ `์ฐธ์กฐ``ํ ๋ ์ฌ์ฉ๋๋ React Hook์ด๋ค.
React ๊ณต์๋ฌธ์์ ๋ฐ๋ฅด๋ฉด, useRef Hook์ ํฌ๊ฒ ์ธ ๊ฐ์ง ์ํฉ์์ ์ฌ์ฉ๋๋ค.
1. Referencing a value with a ref 2. Manipulating the DOM with a ref 3. Avoiding recreating the ref contents
๋ง์ ๋ด์ฉ์ด ์๊ฒ ์ง๋ง, ์ค๋์ ๊ณต์๋ฌธ์์ ์๋ Stopwatch ์์ ์ฝ๋๋ฅผ ํตํด useRef๋ฅผ ์ฌ์ฉํ๊ณ useState์ ๋น๊ตํด ๋ณด์๋ค.
Stopwatch code ๐ก
import { useState, useRef } from "react";
export default function Stopwatch() {
const [startTime, setStartTime] = useState(null);
const [now, setNow] = useState(null);
const intervalRef = useRef(null);
console.log("๋ ์คํ๋์ด 1");
// 2. handleStart ํจ์๊ฐ ์คํ๋จ
function handleStart() {
// 3. startTime๋ผ๋ ์ํ๋ฅผ ํ์ฌ ์๊ฐ์ผ๋ก ์
๋ฐ์ดํธ
setStartTime(Date.now());
// 4. now๋ผ๋ ์ํ๋ฅผ ํ์ฌ ์๊ฐ์ผ๋ก ์
๋ฐ์ดํธ
setNow(Date.now());
// 5. cleaerInterval์ ์ฌ์ฉํ์ฌ ์คํ๋๋ ์ธํฐ๋ฒ์ ์ค์ง, ๋จ ์ด๊ธฐ๊ฐ์ด null์ด๊ธฐ์ ์ฒซ ํด๋ฆญ์์๋ ๋ฌด์๋จ
clearInterval(intervalRef.current);
// 6. 0.01์ด๋ง๋ค now๋ฅผ ํ์ฌ ์๊ฐ์ผ๋ก ์
๋ฐ์ดํธํ๋ ์๋ก์ด ์ธํฐ๋ฒ์ ์ค์ ํจ
intervalRef.current = setInterval(() => {
setNow(Date.now());
}, 10);
}
// 12. handleStop ํจ์๊ฐ ์คํ๋จ
function handleStop() {
// 13. ์คํ ์ค์ธ ์ธํฐ๋ฒ์ clearInterval์ ์ฌ์ฉํ์ฌ ์ค์งํจ
clearInterval(intervalRef.current);
}
// 7. secondsPassed ๋ณ์๋ ๊ฒฝ๊ณผํ ์๊ฐ์ ๋ํ๋ด๋ ๋ณ์
let secondsPassed = 0;
console.log(secondsPassed);
console.log("๋ ์คํ๋์ด 2");
// 8. startTime๊ณผ now๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ์๋ง ๊ฐ์ ๊ณ์ฐํจ
if (startTime != null && now != null) {
// 9. ๊ณ์ฐ์ '(now-startTIme)/1000'๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๋ฐ๋ฆฌ์ด ๋จ์์ธ ์๊ฐ์ ์ด ๋จ์๋ก ๋ณํํจ
secondsPassed = (now - startTime) / 1000;
}
console.log(secondsPassed);
console.log("๋ ์คํ๋์ด 3");
return (
<>
{/* 10. ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์์์ ์ธ ์๋ฆฌ๊น์ง ๋ณด์ฌ์ค */}
<h1>Time passed: {secondsPassed.toFixed(3)}</h1>
{/* 1.Start ๋ฒํผ์ ํด๋ฆญํจ */}
<button onClick={handleStart}>Start</button>
{/* 11. Stop ๋ฒํผ์ ํด๋ฆญํจ */}
<button onClick={handleStop}>Stop</button>
</>
);
}

์ฝ๋๋ง๋ค ์ฃผ์์ผ๋ก ์ค๋ช
์ ์ฒจ๋ถํ์ง๋ง, ํต์ฌ์ ์ธ ๋ด์ฉ์ useState๋ ``์ํ๋ฅผ ์
๋ฐ์ดํธ`ํ๊ณ ํด๋น ์ํ์ ๋ํ `๋ ๋๋ง`์ ๊ด๋ฆฌํ๋ ๋ฐ๋ฉด, useRef๋ `๊ฐ์ ์ ์ง`ํ๊ณ ์ปดํฌ๋ํธ์ `๋ ๋๋ง๊ณผ ๊ด๋ จ์ด ์๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ``ํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค๋ ์ ์ด๋ค.

More to read
AI&ML ๊ธฐ์ด
Reference: https://bettermesol.github.io/ml/2019/09/16/ai-ml-dl/AI: ๊ธฐ๊ณ๊ฐ ์ฌ๋์ฒ๋ผ ์๊ฐํ๊ณ ํ๋จํ๊ฒ ๋ง๋๋ ๊ฐ์ฅ ๋์ ๋ฒ์ฃผ์ ๊ธฐ์ ์ ๋๋ค.ML: ๋ฐ์ดํฐ๋ฅผ ํ์ตํ์ฌ ์ค์ค๋ก ๊ท์น์ ์ฐพ์๋ด๋ AI์ ํ ๋ถ์ผ๋ก,
'AI Agent Economy'Novitas : AI Agent๊ฐ ์ง๊ฐ์ ๊ฐ์ง๋ ์ธ์
์ผ๋ง ์ , ๋ฏธ๋์์ ์ฆ๊ถ ๋ฆฌ์์น ๋ฆฌํฌํธ(์ฌํด๋ ์ด๋๋ฆฌ์์ด๋ค: ์์ด์ ํธ ์๋์ Near Automata)๋ฅผ ์ ํ๊ฒ ๋์์ต๋๋ค. AI Agent๋ฅผ ์ธ๊ฐ๊ณผ ํจ๊ปํ ๊ฒฝ์ ์ฃผ์ฒด๋ก ๋ฐ๋ผ๋ณด๋ ์๊ฐ์ ์ ์์ด ์ถฉ๊ฒฉ์ ๋ฐ์๋๋ฌ์ฃ .ํ ๊ฐ์ง ์ง๊ณ ๋์ด๊ฐ ๋ถ๋ถ์ด ์์ต๋๋ค. ์ฐ๋ฆฌ๊ฐ ํํ 'AI'
'ERC-8004'Novitas: AI ์์ด์ ํธ ๊ฒฝ์ ์ฃผ์ฒด
Web 4.0์ ํ ๋ฌธ์ฅ์ผ๋ก ์ ์ํ๋ฉด Sovereign Transact์ ๋๋ค.AI๊ฐ ์ธ๊ฐ์ ํ๋ฝ ์์ด ์ง๊ฐ์ ์์ ํ๊ณ , ๊ฒฐ์ ๋ฅผ ์ํํ๋ฉฐ, ์ธํ๋ผ๋ฅผ ํต์ ํ๋ ์ฃผ๊ถ์ ๊ฒฝ์ ์ฃผ์ฒด๊ฐ ๋๋ ์ธ๊ณ์ ๋๋ค. Web 3.0์ด ๋ธ๋ก์ฒด์ธ ๊ธฐ๋ฐ์ ํ์ค์ํ๋ฅผ ์คํํ๋ค๋ฉด, Web 4.0์ ๊ทธ