jtwjs Dev Wiki
  • DEV_ROAD
    • ๐Ÿ’ช๐Ÿป ์ƒ์กดํ•˜๊ธฐ
    • Week 1
      • ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ธํŒ…
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
      • ๋ฆฌ์—‘ํŠธ
      • Testing Library
      • Parcel & ESLint
    • Week 2
      • JSX
      • Virtual DOM
    • Week 3
      • React Component
      • React State
    • Week 4
      • Express
      • Fetch API & CORS
      • React Hook
      • useRef & Custom Hook
    • Week 5
      • TDD
      • React Testing Library
      • MSW
      • Playwrite
      • Snapshot
    • Week 6
      • Separtion of Concerns
      • Principle
      • DI, (Dependency Injection)
      • Reflect-metadata
      • TSyringe
      • External Store
      • Follow Redux
      • usestore-ts
      • useSyncExternalStore
    • Week 7
      • Routing
      • Routes
      • Router
      • Navigation
    • Week 8
      • Design System
      • Style Basics
      • CSS-in-JS
      • Styled-Components
      • Global Style & Theme
    • Week 9
      • ๊ฐœ๋ฐœํ•˜๊ธฐ ์ „ ์ค€๋น„
      • ์ƒํ’ˆ ๋ชฉ๋ก ํŽ˜์ด์ง€
      • ์ƒํ’ˆ ์ƒ์„ธ ํŽ˜์ด์ง€
      • ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€
    • Week 10
      • ๋กœ๊ทธ์ธ
      • ๋กœ๊ทธ์•„์›ƒ
      • ํšŒ์›๊ฐ€์ž…
      • ์ฃผ๋ฌธ ๋ชฉ๋ก & ์ฃผ๋ฌธ ์ƒ์„ธ
    • Week 11
      • ๋ฐฐ์†ก ์ •๋ณด ์ž…๋ ฅ
      • ํฌํŠธ์› ๊ฒฐ์ œ ์š”์ฒญ
      • ๋ฐฐ์†ก ๋ฐ ๊ฒฐ์ œ ์ •๋ณด ์ „๋‹ฌ
    • Week 12
      • ๊ด€๋ฆฌ์ž ์›น์‚ฌ์ดํŠธ๊ฐœ๋ฐœ์‹œ์ž‘
  • DEV_NOTE
    • TypeScript
      • ๊ธฐ๋ณธ์  ๋ฌธ๋ฒ•
        • Enum
        • ๋‹คํ˜•์„ฑ
          • Untitled
        • ๊ตฌ์กฐ์  ํƒ€์ดํ•‘
        • ์ œ๋„ˆ๋ฆญ ํƒ€์ž…
        • ์ปจ๋””์…”๋„ ํƒ€์ž…
        • ํ•จ์ˆ˜ ๋ฉ”์„œ๋“œ ํƒ€์ดํ•‘
        • infer๋กœ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ์ถ”๋ก  ์ง์ ‘ ํ™œ์šฉ
        • ์žฌ๊ท€ ํƒ€์ž…
        • ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…
        • ์ถ”๊ฐ€์ ์ธ ํƒ€์ž… ๊ฒ€์‚ฌ satisfies ์—ฐ์‚ฐ์ž
        • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ฑด๋ง์ฆ
        • ์›์‹œ ์ž๋ฃŒํ˜•์—๋„ ๋ธŒ๋žœ๋”ฉ ๊ธฐ๋ฒ• ์‚ฌ์šฉ ๊ฐ€๋Šฅ
        • ํƒ€์ž… ์ขํžˆ๊ธฐ
        • ์œ ์šฉํ•œ ํƒ€์ž… ๋งŒ๋“ค๊ธฐ
        • ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜
        • ์•ฐ๋น„์–ธํŠธ ์„ ์–ธ๋„ ์„ ์–ธ ๋ณ‘ํ•ฉ์ด ๋œ๋‹ค.
        • ์•ฐ๋น„์–ธํŠธ ์„ ์–ธ๋„ ์„ ์–ธ๋ณ‘ํ•ฉ์ด ๋œ๋‹ค.
    • Testing
      • Unit Testing
      • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์˜ ๋‘ ๋ถ„ํŒŒ
      • ์ข‹์€ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” 4๋Œ€ ์š”์†Œ
      • ํ…Œ์ŠคํŠธ ๋Œ€์—ญ๊ณผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๋™์ž‘
      • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์Šคํƒ€์ผ
      • ๊ฐ€์น˜ ์žˆ๋Š” ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋ฆฌํŒฉํ† ๋ง
      • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ
      • Cross Browsing Testing
      • ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ ์ข…๋ฅ˜
      • React Testing Pattern
      • ํ”„๋ก ํŠธ์—”๋“œ ํ…Œ์ŠคํŠธ ์ž…๋ฌธ
        • ํ…Œ์ŠคํŠธ ๋ฒ”์œ„
        • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ๊ฒ€์ฆ
        • Mock
        • UI ์ปดํฌ๋„ŒํŠธ ํ…Œ์ŠคํŠธ
        • ํ…Œ์ŠคํŠธ ์ปค๋ฒ„๋ฆฌ์ง€
        • ์›น ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ
        • MSW
        • ์Šคํ† ๋ฆฌ๋ถ
        • ์‹œ๊ฐ์  ํšŒ๊ท€ ํ…Œ์ŠคํŠธ
        • E2E ํ…Œ์ŠคํŠธ
        • Github Actions ์„ค์ •
        • ๊นƒํ—ˆ๋ธŒ ์•ก์…˜์—์„œ E2E
      • ์‹œํ”„ํŠธ ๋ ˆํ”„ํŠธ
        • ํ…Œ์ŠคํŠธ ๊ธฐ๋ณธ์ค‘์˜ ๊ธฐ๋ณธ
        • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ
        • ์ฝ”๋“œ ๋ณต์žก๋„
        • ๋ฆฌํŒฉํ„ฐ๋ง
        • ์ฝ”๋“œ ๋ฆฌ๋ทฐ
        • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ํŒจํ„ด
        • ์‹œ์Šคํ…œ ํ…Œ์ŠคํŠธ์˜ ์ž๋™ํ™”
        • ํƒ์ƒ‰์  ํ…Œ์ŠคํŠธ
      • Test Tip
      • vitest
      • playwright
      • Test Data Generator
      • MSW
    • Algorithm
      • coding test
      • Data Structure
    • Next.js
      • Data Fetching
      • Hydration
      • Next 13
      • Optimization
      • Next 15
    • Tailwind
      • Tailwind CSS
      • Theme
      • Directives
      • Tool
      • Design System
    • Storybook
      • Storybook
      • CSF3
      • CDD
      • Headless Component
    • Funtional Programming
      • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      • ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ
      • ๋ถ€์ˆ˜ํšจ๊ณผ
      • ํ•จ์ˆ˜ ํ•ฉ์„ฑ
      • ์ œ๋„ˆ๋ฆญ ํƒ€์ž… ํ™œ์šฉํ•˜๊ธฐ
      • ์•”๋ฌต์  ์ž…์ถœ๋ ฅ
      • ์•ก์…˜๊ณผ ๊ณ„์‚ฐ, ๋ฐ์ดํ„ฐ
      • ๊ณ„์ธตํ˜• ์„ค๊ณ„
      • ํ˜ธ์ถœ ๊ทธ๋ž˜ํ”„
      • ํ•จ์ˆ˜ํ˜• ์„ค๊ณ„
      • ๋ถˆ๋ณ€์„ฑ
      • ์ผ๊ธ‰ ํ•จ์ˆ˜
      • ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ
    • Git
      • Github Actions
      • Conflict
      • Branch ์ „๋žต
    • Contents Format
      • Audio
    • 3D Graphic
      • 3D keyword
      • Three.js
      • Geometry
      • Material
      • Light
      • Camera
      • Decal
      • Rotation
      • Text
      • Shadow
      • Fog
      • Post Processing
      • Animation
      • Math
        • Vector Space
        • ๋ฒกํ„ฐ์˜ ์—ฐ์‚ฐ
        • ํšŒ์ „ ๊ณ„์‚ฐ
      • 3D ์ปจํ…์ธ ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ณผ์ •
      • R3F
      • Env
      • Scene
      • Transform
      • R3F
      • Interaction & Raycast
      • Rendering Algorithnm
      • Blender
      • Blender
    • Accessibility
      • ์ ‘๊ทผ์„ฑ์ด๋ž€
    • Interactive Web
      • Parallax
      • Canvas
      • requestAnimationFrame
      • Effect
      • HSL
      • React.js + Canvas
      • Matter.js
    • AWS
      • DevOps
      • Amplify
      • S3
      • ํด๋ผ์šฐ๋“œ ์ปดํ“จํŒ…
        • ์˜จํ”„๋ ˆ๋ฏธ์Šค์™€ ํด๋ผ์šฐ๋“œ
        • ํด๋ผ์šฐ๋“œ ๋„์ž…ํšจ๊ณผ
        • ํด๋ผ์šฐ๋“œ ์ปดํ“จํŒ…์˜ ๋ฒ”์œ„
        • ์ปดํ“จํŒ… ์˜ต์…˜
          • EC2 - Virtual Machin
          • ECS, EKS - Container
          • Lambda - Serverless
        • ๋„คํŠธ์›Œํฌ ๊ฐ€์ƒํ™”
        • ์Šคํ† ๋ฆฌ์ง€
        • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
        • ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘
        • ๋จธ์‹  ๋Ÿฌ๋‹ ์˜์—ญ
        • IoT ์˜์—ญ
        • ๋ธ”๋ก์ฒด์ธ ์˜์—ญ
      • ํด๋ผ์šฐ๋“œ ์•„ํ‚คํ…์ฒ˜ ์„ค๊ณ„
    • Network
      • Web Server & WAS
    • System Design
      • System Design
      • Component
      • ์˜์กด์„ฑ์„ ๋ฐฐ์ œํ•œ ๊ฐœ๋ฐœ
      • Error Handling
      • Architecture
        • ๋ชจ๋…ธ๋กœํ‹ฑ ์•„ํ‚คํ…์ฒ˜
        • Clean Architecture
        • Layered Architecture
        • ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜
      • ์ƒํ™ฉ์„ ํŒŒ์•…ํ•˜๋Š” ๋ฉ”ํƒ€์ธ์ง€
      • ์ค‘๋ณต ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๊ธฐ
      • Monorepo Arhitecture
        • ๋ชจ๋…ธ๋ ˆํฌ ์šด์˜๊ณผ ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…
        • Module Federation
      • ์ฝ”๋“œ ๋ณ‘๋ชฉ์ง€์ 
      • API ๋Œ€์‘
      • ๊ณตํ†ต ์ฝ”๋“œ
      • Infra ๊ตฌ์ถ•
      • ๋ชจ๋“ˆ ๊ธฐ๋ฐ˜์˜ ๊ฐœ๋ฐœ ๋ฐฉ์‹
      • Design System
        • ์ตœ์†Œ ์ˆ˜์ค€์˜ ์•„ํ‚คํ…์ฒ˜ ์„ค์ •
        • ๋” ํšจ์œจ์ ์ธ ๋””์ž์ธ์‹œ์Šคํ…œ ๋งŒ๋“ค๊ธฐ
        • ๋””์ž์ธ ์‹œ์Šคํ…œ๊ณผ UI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชฉ์ 
        • ๋””์ž์ธ ํ† ํฐ
      • ํšจ์œจ์ ์ธ ์—…๋ฌด
        • ์—…๋ฌด ํ”„๋กœ์„ธ์Šค ๋ณ‘๋ชฉ ํŒŒ์•…
      • Clean Code
      • Design Pattern
        • CQRS Pattern
        • Strangler Fig Pattern
        • ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒจํ„ด
        • ์ปค๋งจ๋“œ ํŒจํ„ด
        • ์ „๋žต ํŒจํ„ด
        • ์˜ต์ €๋ฒ„ ํŒจํ„ด
      • A/B ํ…Œ์ŠคํŒ…
      • ๋Œ€๊ทœ๋ชจ ๋ฆฌ์—‘ํŠธ ์›น์•ฑ ๊ฐœ๋ฐœ
        • ๋ณต์žก์„ฑ ๊ด€๋ฆฌ
        • ๋ชจ๋“ˆ์„ฑ
        • ์„ฑ๋Šฅ
        • ๋””์ž์ธ ์‹œ์Šคํ…œ
        • ๋ฐ์ดํ„ฐ ํŒจ์นญ
        • ์ƒํƒœ ๊ด€๋ฆฌ
        • ๊ตญ์ œํ™”
        • ์ฝ”๋“œ ์กฐ์งํ™”ํ•˜๊ธฐ
        • ๊ฐœ์ธํ™” A/B ํ…Œ์ŠคํŒ…
        • ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์›น ์•„ํ‚คํ…์ฒ˜
        • ํ…Œ์ŠคํŒ…
        • ํˆด๋ง
        • ๊ธฐ์ˆ ์  ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜
        • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
        • ๋ผ์šฐํŒ…
        • ์‚ฌ์šฉ์ž ์ค‘์‹ฌ API ๋””์ž์ธ
        • ๋ฆฌ์•กํŠธ ๋ฏธ๋ž˜
    • Performance
      • React DevTools
      • Component ์ตœ์ ํ™”
      • Page Load
      • API
    • MFA
      • MSA
      • MFA ๋„์ž…ํ•˜๊ธฐ
      • Monorepo
        • Monorepo Tool
        • Yarn Berry Workspace
        • Turborepo
      • MFA Composition
      • SPA ํ†ตํ•ฉ
      • Design System
      • Package Manager
        • Yarn
        • pnpm
      • Transpiler & Bundler
        • Babel
        • Rollup
        • esbuild
        • swc
        • Webpack
        • Vite
      • ๋ถ„ํ•ด์™€ ํ†ตํ•ฉ์„ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ธฐ์ˆ  ๋น„๊ต
    • State Management
      • Zustand
    • React v18
      • Automatic batching
      • Suspense
      • Transition
    • SEO
      • Search Engine Optimization
      • Open Graph Element
      • Metadata
    • FE Develop
      • Scrubbing
      • Clipboard
    • Refactoring
      • ๋ฆฌํŒฉํ† ๋ง ๊นŠ๊ฒŒ ๋“ค์—ฌ๋‹ค๋ณด๊ธฐ
      • ๊ธด ์ฝ”๋“œ ์กฐ๊ฐ๋‚ด๊ธฐ
      • ํƒ€์ž… ์ฝ”๋“œ ์ฒ˜๋ฆฌํ•˜๊ธฐ
      • ์œ ์‚ฌํ•œ ์ฝ”๋“œ ์œตํ•ฉํ•˜๊ธฐ
      • ๋ฐ์ดํ„ฐ ๋ณดํ˜ธ
      • ์ฝ”๋“œ ์ถ”๊ฐ€ ๋ฐ ์ œ๊ฑฐ
    • OAuth 2.0
    • Analytics
      • Mixpanel
    • ETC
      • VSCode
    • React Hook In Action
      • useContext & Provider
      • ์ปค์Šคํ…€ ํ›…
      • ์ฝ”๋“œ ๋ถ„ํ• ํ•˜๊ธฐ with Suspense, lazy
      • Suspense์™€ ์ด๋ฏธ์ง€ ์ ์žฌํ•˜๊ธฐ
      • useTransition, uesDeferredValue
      • SuspenseList
Powered by GitBook
On this page
  • External Store
  • forceUpdate
  • React ์ธก forceUpdate์— ๋Œ€ํ•œ ์ž…์žฅ
  • Props Drilling
  • Props Drilling์˜ ๋ฌธ์ œ์ 
  • Context API
  • Context ์ ์šฉ ์ฝ”๋“œ
  • External Store ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ with Tsyringe
  • ObjectStore.
  • CounterStore
  • useObjectStore
  • useCounterStore
  • Usage
  1. DEV_ROAD
  2. Week 6

External Store

PreviousTSyringeNextFollow Redux

Last updated 2 years ago

External Store

๋ฆฌ์•กํŠธ ์™ธ๋ถ€์— Store(์ €์žฅ์†Œ)๋ฅผ ๋‘๊ณ  ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•จ์œผ๋กœ์จ, ์ƒํƒœ๋ฅผ ์ œ์–ดํ•˜๋Š” ๊ด€์‹ฌ์‚ฌ์™€ UI(์ปดํฌ๋„ŒํŠธ)์˜ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌ

React ์ปดํฌ๋„ŒํŠธ ์ž…์žฅ์—์„œ๋Š” โ€œ์ „์—ญโ€์ฒ˜๋Ÿผ ์—ฌ๊ฒจ์ง„๋‹ค.

โ€œProp Drillingโ€ ๋ฌธ์ œ๋ฅผ ์šฐ์•„ํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜(React๋กœ ํ•œ์ •ํ•˜๋ฉด Context๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค).

External โ†’ (React) ์™ธ๋ถ€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

์ผ๋ฐ˜์ ์ธ ์•„ํ‚คํ…์ฒ˜์—์„œ๋Š” UI(React)๊ฐ€ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์ด์ง€๋งŒ ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” Extenral์€ ์•ˆ์ด๋ƒ ๋ฐ–์ด๋ƒ๋ผ๋Š” ๊ด€์ ์ด ์•„๋‹ˆ๋ผ React ์ž…์žฅ์—์„œ ์™ธ๋ถ€๋ฅผ ์˜๋ฏธ

forceUpdate

React๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ React ์™ธ๋ถ€๋ฅผ ํ†ตํ•ด ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ useState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ํ™”๋ฉด์„ ๋ฆฌ๋ Œ๋”๋ง ์‹œํ‚ฌ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•œ๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ•์ œ์ ์œผ๋กœ ๋ฆฌ๋ Œ๋”๋งํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

function useForceUpdate() {
  const [, setState] = useState({});
  return useCallback(() => setState({}), []);
}

React ๊ณต์‹๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š” forceUpdate ์ฝ”๋“œ

๊ฐ€๋Šฅํ•˜๋ฉด ํ•ด๋‹น ํŒจํ„ด์„ ํ”ผํ•˜๋ผ๊ณ  ์ฃผ์˜๋ฅผ ์ฃผ๊ณ  ์žˆ๋‹ค.

const [ignored, forceUpdate] = useReducer(x => x + 1, 0);

function handleClick() {
  forceUpdate();
}

useReducer

React์—์„œ ์ œ๊ณตํ•˜๋Š” ์ƒํƒœ ๊ด€๋ฆฌ ํ›…์ด๋ฉฐ, useState์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง์„ ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€๋กœ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

useReducer๊ฐ€ ๊ธฐ๋ณธํ˜•์ด๊ณ  useState๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ useReudcer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ณต์œ ๋˜๋Š” ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ ๋„์›€์ด ๋œ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•Œ๊ณ ์žˆ๋Š” Redux์˜ ๊ทธ๊ฒƒ๊ณผ ๋น„์Šทํ•จ

const [state, dispatch] = useReducer(reducer, initialState);
  • dispatch: ์•ก์…˜์„ ๋ณด๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

  • reducer: ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜

๋ฆฌ์—‘ํŠธ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ƒํƒœ๊ด€๋ฆฌ(useState, useReduer, props, context)๋ฅผ Internal Store๋ผ๊ณ  ํ•œ๋‹ค.

useCallback

React์—์„œ ์ œ๊ณตํ•˜๋Š” ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ํ›…์ด๋ฉฐ, ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ์บ์‹œํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•œ๋‹ค.

๋งค ๋ Œ๋”๋ง ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ด์ „์— ์ƒ์„ฑ๋œ ํ•จ์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋„๋ก ๋ณด์žฅํ•จ

์ž์‹์ปดํฌ๋„ŒํŠธ์— props๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ props์˜ ๋ณ€ํ™”๋กœ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฆฌ๋ Œ๋”๋ง ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

useCallback๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์„œ ์ตœ์ ํ™” ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋˜ ๋‹ค๋ฅธ ์„ฑ๋Šฅ ์ตœ์ ํ™” ํ›…์ธ React.memo() ์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผ ์ตœ์ ํ™” ๋œ๋‹ค๋Š” ๊ฒƒ์— ์œ ์˜

React.memo Hooks

์ด์ „์— ๋ Œ๋”๋ง๋œ ๊ฒฐ๊ณผ๋ฅผ ์บ์‹œํ•˜๊ณ , ๋‹ค์Œ ๋ Œ๋”๋ง ์‹œ ํ˜„์žฌ props์™€ ์–•๊ฒŒ ๋น„๊ต(๋ณ€๊ฒฝ๊ฐ€๋Šฅ)ํ•ด์„œ ๊ฐ™๋‹ค๋ฉด ๋ฆฌ๋ Œ๋”๋ง์„ ํ•˜์ง€ ์•Š๊ณ  ์บ์‹œ๋œ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹์œผ๋กœ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€ํ•œ๋‹ค.

const callbackFn = useCallback(() => {
  doSomething
}, [dependency])

useEffect์˜ ์˜์กด์„ฑ ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ๋ฐ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ฐธ์กฐํ•˜๋Š” ์ƒํƒœ๋‚˜ props ์ค‘์—์„œ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์žˆ์„๋•Œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ์ง€์ •ํ•œ๋‹ค.

Props Drilling

๋ถ€๋ชจ์—์„œ React ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ์ค‘์ฒฉ๋œ ์ž์‹์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ค‘๊ฐ„ ๋‹จ๊ณ„์˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ props๋ฅผ ๋‹จ์ˆœํžˆ ์ž์‹์—๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋Š” ํ˜•ํƒœ๋ฅผ props drilling์ด๋ผ ํ•จ

function Parent() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <Children count={count} setCount={setCount} />
    </div>
  )    
}

function Children({count, setCount}) {
  return (
    <div>
      <ChildrenChildren count={count} setCount={setCount} />
    </div>
  )    
}

function ChildrenChildren({count, setCount}) {
  return (
    <div>
      <p>{`count: ${count}`}</p>
      <button type="button" onClick={() => setCount((prev) => prev + 1)}>Increase</button>
    </div>
  )    
}

Props Drilling์˜ ๋ฌธ์ œ์ 

  • ์ปดํฌ๋„ŒํŠธ ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด props๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

  • ์ฝ”๋“œ๊ฐ€ ๊ธˆ๋ฐฉ ๋ณต์žกํ•ด์ง€๊ณ  ์‹ค์ˆ˜ํ•˜๊ธฐ ์‰ฝ๋‹ค.

Context API

React์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” Context API Hooks๋กœ Props Drilling์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

Context์˜ ์‚ฌ์šฉ ๋ชฉ์ ์€ ๋ณต์žกํ•˜๊ฒŒ "์ค‘์ฒฉ ๋œ" ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ

Context๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉ์ด ์–ด๋ ค์šฐ๋ฏ€๋กœ ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.


import {createContext, useContext, useState, useMemo} from 'react';

interface CounterContextValue {
  count: number;
  setCount: () => void;
}

export const CounterContext = createContext({} as CounterContextValue);

export function useCounter() {
  const contextValue = useContext(CounterContext);
  
  if (!contextValue) {
    throw new Error('useCounter must be used within CounterProvider');
  }
  
  return contextValue;
}

export default CounterProvider({children}: React.PropsWithChildren) {
  const [count, setCount] = useState(0);
  
  const value = useMemo(() => ({count, setCount}), [count, setCount])
  
  return (
    <CounterContext.Provider value={value}>
      {children}
    </CounterContxt.Provider>
  )
}

Context ์ ์šฉ ์ฝ”๋“œ

๋„˜๊ฒจ์ฃผ๋Š” Props๋“ค์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•˜๋‹ˆ ๊น”๋”ํ•ด์กŒ๋‹ค.

function Parent() {
  return (
    <CounterProvider>
      <div>
        <Children/>
      </div>
    </CounterProvider>
  )    
}

function Children() {
  return (
    <div>
      <ChildrenChildren/>
    </div>
  )    
}

function ChildrenChildren() {
  const {count, setCount} = useCounter();
  
  return (
    <div>
      <p>{`count: ${count}`}</p>
      <button type="button" onClick={() => setCount((prev) => prev + 1)}>Increase</button>
    </div>
  )    
}

Context API + useReduecr ์กฐํ•ฉ์œผ๋กœ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ‰๋‚ด๋‚ผ์ˆ˜ ์žˆ์ง€๋งŒ ๊ด€๋ฆฌ๋˜๋Š” ์ปจํ…์ŠคํŠธ ๊ฐ’ ์ค‘ ์ผ๋ถ€๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜์—ˆ์„ ๋•Œ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์€ ๋‹ค๋ฅธ ์ƒํƒœ๊ฐ’์— ์—ฐ๊ฒฐ๋œ ์ปดํฌ๋„ŒํŠธ ๋˜ํ•œ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜๋Š” Side Effect๊ฐ€ ์žˆ๋‹ค.

External Store ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ with Tsyringe

์—ฌ๋Ÿฌ ์Šคํ† ์–ด๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌ๋˜๋Š” extenral store๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ ํ•ด๋ณด์ž.

ObjectStore.

์—ฌ๋Ÿฌ ์Šคํ† ์–ด์˜ ๋ฒ ์ด์Šค๊ฐ€ ๋˜๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค

type Listener = () => void;

export default abstract class ObjectStore {
  private listeners = new Set<Listener>();

  protected publish() {
    this.listeners.forEach((listener) => listener());
  }

  addListener(listener: Listener) {
    this.listeners.add(listener);
  }

  removeListener(listener: Listener) {
    this.listeners.delete(listener);
  }
}

CounterStore

import { singleton } from 'tsyringe';

import ObjectStore from './objectStore';

@singleton()
export default class CounterStore extends ObjectStore {
  counter = 0;

  increase(step = 1) {
    this.counter += step;
    this.publish();
  }

  decrease(step = 1) {
    this.counter -= step;
    this.publish();
  }
}

useObjectStore

import { useEffect } from 'react';

import ObjectStore from '../stores/objectStore';

import useForceUpdate from './useForceUpdate';

export default function useObjectStore<T extends ObjectStore>(store: T): T {
  const forceUpdate = useForceUpdate();

  useEffect(() => {
    store.addListener(forceUpdate);

    return () => {
      store.removeListener(forceUpdate);
    };
  }, [store, forceUpdate]);

  return store;
}

useCounterStore

import { container } from 'tsyringe';

import CounterStore from '../stores/counterStore';

import useObjectStore from './useObjectStore';

export default function useCounterStore() {
  const store = container.resolve(CounterStore);

  return useObjectStore(store);
}

Usage

CounterController.tsx

import useCounterStore from '../hooks/useCounterStore';

export default function CounterController() {
  const store = useCounterStore();

  const handleClickIncrease = (step?: number) => () => {
    store.increase(step);
  };

  const handleClickDecrease = (step?: number) => () => {
    store.decrease(step);
  };

  return (
    <div>
      <button type="button" onClick={handleClickIncrease(10)}>
        Increase 10
      </button>
      <button type="button" onClick={handleClickIncrease()}>
        Increase
      </button>
      <button type="button" onClick={handleClickDecrease(10)}>
        Decrease 10
      </button>
      <button type="button" onClick={handleClickDecrease()}>
        Decrease
      </button>
    </div>
  );
}

Counter.tsx

import useCounterStore from '../hooks/useCounterStore';

export default function Counter() {
  const { counter } = useCounterStore();

  return (
    <div>
      <p>{`counter: ${counter}`}</p>
    </div>
  );
}

์ด๋Ÿฐ ์ ‘๊ทผ์„ ์ž˜ ํ•˜๋ฉด, React๊ฐ€ UI๋ฅผ ๋‹ด๋‹นํ•˜๊ณ , ์ˆœ์ˆ˜ํ•œ TypeScript(๋˜๋Š” JavaScript)๊ฐ€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š”, ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ(Separation of Concerns)๋ฅผ ๋ช…ํ™•ํžˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž์ฃผ ๋ฐ”๋€Œ๋Š” UI ์š”์†Œ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ๋Œ€์‹ , ์˜ค๋ž˜ ์œ ์ง€๋˜๋Š”(๋ฐ”๋€Œ๋ฉด ์น˜๋ช…์ ์ธ) ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์œ ์ง€๋ณด์ˆ˜์— ๋„์›€์ด ๋˜๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์น˜๋ฐ€ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

React ์ธก forceUpdate์— ๋Œ€ํ•œ ์ž…์žฅ