๐Ÿง—
JIN-DevNote
  • ํ”„๋ก ํŠธ์—”๋“œ ์ƒ์กด์ฝ”์Šค 3๊ธฐ
  • 0์ฃผ์ฐจ
    • 1.์™€์นดํƒ€์ž„ ์„ค์น˜
    • 2.git-training
  • 1์ฃผ์ฐจ
    • 1.๊ฐœ๋ฐœํ™˜๊ฒฝ
    • 2.Typescript
    • 3.React
    • 4.Testing Library
    • 5.Parcel & ESLint
  • 2์ฃผ์ฐจ
    • 1.JSX
  • 3์ฃผ์ฐจ
    • 1.React Component
    • 2.React State
  • 4์ฃผ์ฐจ
    • 1.Express
    • 2.Fetch API & Cors
    • 3.React์˜ hook
    • 4.useRef & Custom hook
    • 5.usehooks-ts
  • 5์ฃผ์ฐจ
    • 1.Tdd
    • 2.React Testing library
    • 3.MSW
    • 4.Playwright
  • 6์ฃผ์ฐจ
    • 1.External Store
    • 2.Tsyringe
    • 3.Redux ๋”ฐ๋ผํ•˜๊ธฐ
    • 4.usestore-ts
  • 7์ฃผ์ฐจ
    • 1.Routing
    • 2.Routes
    • 3.Router
    • 4.Navigation
  • 8์ฃผ์ฐจ
    • 1.Design System
    • 2.Style Basics
    • 3.CSS in JS
    • 4.styled-components
    • 5.props์™€ attrs
    • 6.Global Style & Theme
Powered by GitBook
On this page
  • useRef
  • Hook์˜ ๊ทœ์น™
  • Reference
  1. 4์ฃผ์ฐจ

4.useRef & Custom hook

useRef

Hook์˜ ๊ทœ์น™

React์˜ Custom Hooks๋Š” ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋…ผ๋ฆฌ๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์—ฌ๋Ÿฌ ๊ตฌ์„ฑ ์š”์†Œ ๊ฐ„์— ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ๋‹ค์Œ์€ ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ๋ฅผ ๋งŒ๋“ค ๋•Œ ์—ผ๋‘์— ๋‘์–ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์ด๋‹ค.

  1. ํ•จ์ˆ˜ ์ด๋ฆ„์„ 'use'๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ๊ทœ์น™์— ๋”ฐ๋ผ React์˜ ๋ชจ๋“  ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ๋Š” 'use'๋ผ๋Š” ๋‹จ์–ด๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ›„ํฌ๋กœ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  2. ์‚ฌ์šฉ์ž ์ •์˜ ํ›„ํฌ ๋‚ด์—์„œ ๋‹ค๋ฅธ ํ›„ํฌ ์‚ฌ์šฉ: ์‚ฌ์šฉ์ž ์ •์˜ ํ›„ํฌ๋Š” ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉ์ž ์ •์˜ ํ›„ํฌ ๋‚ด์—์„œ useState, useEffect ๋ฐ useContext์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ํ›„ํฌ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  3. ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ์˜ ์ตœ์ƒ์œ„ ์ˆ˜์ค€์—์„œ๋งŒ ํ›„ํฌ ํ˜ธ์ถœ: ํ›„ํฌ๋Š” ๋‚ด๋ถ€ ๋ฃจํ”„, ์กฐ๊ฑด ๋˜๋Š” ์ค‘์ฒฉ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ตฌ์„ฑ ์š”์†Œ ๋˜๋Š” ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ์˜ ์ตœ์ƒ์œ„ ์ˆ˜์ค€์—์„œ๋งŒ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋Š” ์ฝœ๋ฐฑ ๋˜๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด์—์„œ ํ›„ํฌ๋ฅผ ํ˜ธ์ถœํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.

  4. ๋ฐฐ์—ด ๋˜๋Š” ๊ฐœ์ฒด ๋ฐ˜ํ™˜: ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ๋Š” ํ•ญ์ƒ ๋ฐฐ์—ด ๋˜๋Š” ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ›„ํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ๊ฐ’์„ ์‰ฝ๊ฒŒ ๋ถ„ํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  5. ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ ๋ฌธ์„œํ™”: ์ฝ”๋“œ์˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋‚˜ ๊ตฌ์„ฑ ์š”์†Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ๋ฅผ ๋ช…ํ™•ํ•œ ์ฃผ์„์œผ๋กœ ๋ฌธ์„œํ™”ํ•˜๊ณ  ๊ธฐ๋Šฅ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•, ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋˜๋Š” ๋ฐ˜ํ™˜ ๊ฐ’์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ๋‹ค.

  6. ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ ํ…Œ์ŠคํŠธ: ์‚ฌ์šฉ์ž ์ง€์ • ํ›„ํฌ๋ฅผ ์ฒ ์ €ํžˆ ํ…Œ์ŠคํŠธํ•˜์—ฌ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜๊ณ  ๊ทน๋‹จ์ ์ธ ๊ฒฝ์šฐ๋‚˜ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

usehooks-ts์˜ useBoolean ์ปค์Šคํ…€ ํ›…

import { Dispatch, SetStateAction, useCallback, useState } from 'react'

interface UseBooleanOutput {
  value: boolean
  setValue: Dispatch<SetStateAction<boolean>>
  setTrue: () => void
  setFalse: () => void
  toggle: () => void
}

function useBoolean(defaultValue?: boolean): UseBooleanOutput {
  const [value, setValue] = useState(!!defaultValue)

  const setTrue = useCallback(() => setValue(true), [])
  const setFalse = useCallback(() => setValue(false), [])
  const toggle = useCallback(() => setValue(x => !x), [])

  return { value, setValue, setTrue, setFalse, toggle }
}

export default useBoolean

์‚ฌ์šฉ

import { useBoolean } from 'usehooks-ts'

export default function Component() {
  const { value, setValue, setTrue, setFalse, toggle } = useBoolean(false)

  // Just an example to use "setValue"
  const customToggle = () => setValue((x: boolean) => !x)

  return (
    <>
      <p>
        Value is <code>{value.toString()}</code>
      </p>
      <button onClick={setTrue}>set true</button>
      <button onClick={setFalse}>set false</button>
      <button onClick={toggle}>toggle</button>
      <button onClick={customToggle}>custom toggle</button>
    </>
  )
}

Reference

Previous3.React์˜ hookNext5.usehooks-ts

Last updated 2 years ago

Reusing Logic with Custom Hooks(์ปค์Šคํ…€ ํ›…์œผ๋กœ ๋กœ์ง ์žฌ์‚ฌ์šฉ)
์ž์‹ ๋งŒ์˜ Hook ๋งŒ๋“ค๊ธฐ