External Store

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 ์š”์†Œ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ๋Œ€์‹ , ์˜ค๋ž˜ ์œ ์ง€๋˜๋Š”(๋ฐ”๋€Œ๋ฉด ์น˜๋ช…์ ์ธ) ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์œ ์ง€๋ณด์ˆ˜์— ๋„์›€์ด ๋˜๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์น˜๋ฐ€ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Last updated