[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
ํ๋ก ํธ์๋์ ๋ฐฑ์๋ ์ฌ์ด
HTTP ์ํ ์ฝ๋๋ ํ๋ก ํธ์๋์์ ๋ฐฑ์๋๋ก ๋ณด๋๋ ์์ฒญ์ ์ํ ๊ฒฐ๊ณผ๋ฅผ ์๋ฏธํ๋ ์ผ์ข ์ ์ฝ์์ด๋ฉฐ, API๋ฅผ ๊ตฌ์ฑํ๋ ํต์ฌ ์์ ์ค ํ๋์ ๋๋ค. ์ํ ์ฝ๋์ ๊ด๋ จํ์ฌ, ๋ฐฑ์๋๋ ์ ๋ชจ๋ฅด๋ ํ๋ก ํธ์๋์ ์ฌํ ์ฌ์ ์ด ์์ต๋๋ค.์๋๋ ์์ฒญ์ด ์คํจํ์์๋, ๋ฐฑ์๋์์ ์ํ ์ฝ๋
JWTํ ํฐ ๊ด๋ฆฌ ๋ฐฉ์ ํบ์๋ณด๊ธฐ
0. ๋ค์ด๊ฐ๋ฉฐ ๐ฏ ์๋น์ค์ ์ ๊ทผํ๋ ค๋ ์ฌ์ฉ์๊ฐ ๋๊ตฌ์ธ์ง ํ์ธํ๋ ๊ณผ์ ์ ์ฌ์ฉ์ ์ธ์ฆ์ด๋ผ๊ณ ํฉ๋๋ค. ์ธ์ฆ๋ ์ฌ์ฉ์์๊ฒ ์ฃผ์ด์ง ๊ถํ์ ํ์ธํ๋ ์์ ์ ์ธ๊ฐ๋ผ๊ณ ๋ถ๋ฆ ๋๋ค. ์ด๋ฒ ๊ธ์์๋ ์ธ๊ฐ๋ ๋ค๋ฃจ์ง ์์ต๋๋ค. ์ฌ์ฉ์ ์ธ์ฆ์๋ ๋ง์ ๋ฐฉ์์ด ์์ง๋ง, ์ค๋์ ์ธ์ ์ธ์ฆ ๋ฐฉ
A2AA2A / MCP ๋ฉํฐ ์์ด์ ํธ ์ค์ผ์คํธ๋ ์ด์
0. ๋ค์ด๊ฐ๋ฉฐ โ๏ธ Google for Developers์, ๋ ์คํ ๋ ๊ณต๊ธ๋ง ์๋๋ฆฌ์ค๋ก ์ฎ์ 6๋ ํ๋กํ ์ฝ(MCP, A2A, UCP, AP2, A2UI, AG-UI)์ ๋ํ ๊ฐ์ด๋๊ฐ ๊ฒ์๋ ์ดํ, MCP์ A2A๋ถํฐ ๊ตฌํํด ๋ณด๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์์ต๋