React

[TIL/React] 2023/06/24

useRef ๐ŸŸข Reference ๐ŸŸก ์ปดํฌ๋„ŒํŠธ์˜ ์ตœ์ƒ๋‹จ์—์„œ useRef๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ref๋ฅผ ์„ ์–ธํ•œ๋‹ค. Usage ๐ŸŸก Referencing a value with a ref ๐ŸŸฃ ref๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฒ„ํŠผ์ด ํด๋ฆญ๋œ ํšŸ์ˆ˜๋ฅผ

2023๋…„ 6์›” 24์ผ1min read

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 ๐ŸŸก

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

AI&ML ๊ธฐ์ดˆ

Reference: https&#x3A;//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์€ ๊ทธ