๐Ÿ“Redux, Recoil ๊ฐœ๋…์žก๊ธฐ

๋ฃธ์ž‰ยท2023๋…„ 6์›” 27์ผ
0

cs

๋ชฉ๋ก ๋ณด๊ธฐ
2/13

๐Ÿชข Redux

  • JS ๊ธฐ๋ฐ˜ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ์‚ฌ์šฉ๋Ÿ‰์ด ์••๋„์ ์œผ๋กœ ๋†’์€ ๋งŒํผ, ์•ˆ์ „์„ฑ ๋ณด์žฅ
  • ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ ์ฝ”๋“œ ์–‘์ด ๋„ˆ๋ฌด ๋งŽ์Œ.
    • ์ž‘์€ ๊ธฐ๋Šฅ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ ์—ฌ๋Ÿฌ ์ค„์˜ ์ฝ”๋“œ ํ•„์š”
  • ๋Ÿฌ๋‹ ์ปค๋ธŒ ๋†’์Œ.
  • ํ•˜๋‚˜์˜ ํŒŒ์ผ(store)์—์„œ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฒ„๊ทธ ๋ฐœ์ƒ ์‹œ ์ถ”์ ์ด ์‰ฝ๊ณ  ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ดํ•จ
  • Redux DevTools๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋””๋ฒ„๊น…์ด ์‰ฝ๋‹ค.
    • ์•ฑ์˜ ์ƒํƒœ๊ฐ€ ์–ธ์ œ, ์–ด๋””์„œ, ์™œ, ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ์—ˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ์ถ”์  ๊ฐ€๋Šฅ
  • ๋Œ€๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ์—์„œ ๋งŽ์ด ์“ฐ์ž„

๐Ÿ’กStore, Action, Reducer

๐Ÿ  Store

  • ์ƒํƒœ๊ฐ€ ๊ด€๋ฆฌ๋˜๋Š” ํ•˜๋‚˜์˜ ๊ณต๊ฐ„
    • ์ปดํฌ๋„ŒํŠธ์™€ ๋ณ„๊ฐœ๋กœ Store๋ผ๋Š” ๊ณต๊ฐ„์— ์ƒํƒœ๋ฅผ ๋‹ด์•„๋‘๊ณ , ๊ด€๋ฆฌํ•œ๋‹ค.
    • ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ƒํƒœ๊ฐ€ ํ•„์š”ํ•  ๋•Œ, ์Šคํ† ์–ด์— ์ ‘๊ทผํ•œ๋‹ค.

๐Ÿ“œ Action

  • ์•ฑ์—์„œ Store์— ์šด๋ฐ˜ํ•  ๋ฐ์ดํ„ฐ (์ฃผ๋ฌธ์„œ)
  • JS ๊ฐ์ฒด ํ˜•์‹
    {
      type: 'ACTION_CHANGE_USER', // ํ•„์ˆ˜
      payload: { // ์˜ต์…˜
        name: 'ํ•˜๋‚˜๋ชฌ',
        age: 100
      }
    }

๐Ÿ’Œ Reducer

  • Action์„ Store์— ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ๊ณณ
  • Action โ†’ Reducer์— ์ „๋‹ฌ โ†’ Store์— ์ƒํƒœ ์—…๋ฐ์ดํŠธ
  • Action โ†’ Reducer์— ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” dispatch() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.

โœ… ๊ฒฐ๋ก 

  1. Action ๊ฐ์ฒด โ†’ dispatch() ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌ๋จ
  2. dispatch(Action) ํ†ตํ•ด Reducer ํ˜ธ์ถœ
  3. Reducer โ†’ ์ƒˆ๋กœ์šด Store ์ƒ์„ฑ

Untitled


๐Ÿชข Recoil

  • React ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ์ง๊ด€์ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋น„๊ต์  ์‚ฌ์šฉ์ž ์นœํ™”์ ์ž„.
  • get/ set ์ธํ„ฐํŽ˜์ด์Šค ์‚ฌ์šฉ โ†’ boilerplate-free API ์ œ๊ณต
  • ๋Ÿฌ๋‹ ์ปค๋ธŒ ๋‚ฎ์Œ.
  • data-flow graph**:** ์ƒํƒœ ๋ฐ์ดํ„ฐ๊ฐ€ atom โ†’ selector โ†’ ์ปดํฌ๋„ŒํŠธ ์ˆœ์„œ๋กœ ํ๋ฆ„
  • ์ปดํฌ๋„ŒํŠธ ์ˆ˜์ • ์—†์ด ๋™๊ธฐ/ ๋น„๋™๊ธฐ ์‹ ์ „ํ™˜ ๊ฐ€๋Šฅ

๐Ÿ’ก Atoms, Selectors

๐Ÿซง Atoms

  • ์ƒํƒœ์˜ ๋‹จ์œ„
  • ์—…๋ฐ์ดํŠธ/ ์ฐธ์กฐ ๊ฐ€๋Šฅ
  • atom ์—…๋ฐ์ดํŠธ โ†’ ๊ตฌ๋…๋œ ์ปดํฌ๋„ŒํŠธ๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜์˜ โ†’ ๋ฆฌ๋ Œ๋”๋ง
  • ๋Ÿฐํƒ€์ž„์—์„œ๋„ ์ƒ์„ฑ ๊ฐ€๋Šฅ
  • React์˜ state ๋Œ€์‹  ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ๋™์ผํ•œ atom์ด ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ โ†’ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋Š” ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•จ.
  • Atoms๋Š” atom ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ
const fontSizeState = atom({
  key: 'fontSizeState',
  default: 14,
});

  • ๊ณ ์œ ํ•œ ํ‚ค ๊ฐ’ ํ•„์š”
  • useRecoilState ํ›…์„ ํ†ตํ•ด atom ์ฝ๊ณ  ์“ฐ๊ธฐ ๊ฐ€๋Šฅ
    • useState์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ์ปดํฌ๋„ŒํŠธ ๊ฐ„์˜ ์ƒํƒœ๊ฐ€ ๊ณต์œ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ฐจ์ด.

      function FontButton() {
        const [fontSize, setFontSize] = useRecoilState(fontSizeState);
        return (
          <button onClick={() => setFontSize((size) => size + 1)} style={{fontSize}}>
            Click to Enlarge
          </button>
        );
      }

๐Ÿ‘€ Selectors

  • atoms๋‚˜ ๋‹ค๋ฅธ selectors๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„๋“ค์ด๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜
  • atom ์ƒํƒœ ๊ฐ’์„ ๋™๊ธฐ/ ๋น„๋™๊ธฐ ๋ฐฉ์‹์„ ํ†ตํ•ด ๋ณ€ํ™˜
  • ์ƒ์œ„ atoms/ selectors ์—…๋ฐ์ดํŠธ โ†’ ํ•˜์œ„์˜ selector๋„ ๋‹ค์‹œ ์‹คํ–‰๋จ
  • ์ฐธ์กฐ ๊ฐ€๋Šฅ
  • selectors ๋ณ€๊ฒฝ โ†’ ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง
  • ์ตœ์†Œํ•œ์˜ ์ƒํƒœ ์ง‘ํ•ฉ๋งŒ atoms์— ์ €์žฅ โ†’ ๋‹ค๋ฅธ ๋ชจ๋“  ํŒŒ์ƒ ๋ฐ์ดํ„ฐ โ†’ selectors์— ๋ช…์‹œํ•œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํšจ์œจ์  ๊ณ„์‚ฐ ๊ฐ€๋Šฅ
  • ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์„ ํ•„์š”๋กœ ํ•˜๋Š”์ง€, ์–ด๋–ค ์ƒํƒœ์— ์˜์กดํ•˜๋Š”์ง€ ์ถ”์ 
  • ์ปดํฌ๋„ŒํŠธ์˜ ๊ด€์ )) selectors, atoms๋Š” ๋™์ผํ•œ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ€์ง โ†’ ์„œ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅ
  • Selectors๋Š” selector ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ
const fontSizeLabelState = selector({
  key: 'fontSizeLabelState',
  get: ({get}) => {
		// fontSizestate๋ผ๋Š” ํ•˜๋‚˜์˜ atom์— ์˜์กด์„ฑ์„ ๊ฐ€์ง
    const fontSize = get(fontSizeState);
    const unit = 'px';

    return `${fontSize}${unit}`;
  },
});
function FontButton() {
  const [fontSize, setFontSize] = useRecoilState(fontSizeState);
	// Selectors๋Š” useRecoilValue() ํ†ตํ•ด ์ฝ์„ ์ˆ˜ ์žˆ์Œ
	// fontSizeLableState๋Š” writable ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— useRecoilState()๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š์Œ!
  const fontSizeLabel = useRecoilValue(fontSizeLabelState);

  return (
    <>
      <div>Current font size: ${fontSizeLabel}</div>

      <button onClick={setFontSize(fontSize + 1)} style={{fontSize}}>
        Click to Enlarge
      </button>
    </>
  );
}


๐Ÿ“š Reference

Redux - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•ฑ์„ ์œ„ํ•œ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ ์ƒํƒœ ์ปจํ…Œ์ด๋„ˆ. | Redux

Redux(๋ฆฌ๋•์Šค)๋ž€? (์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) - ํ•˜๋‚˜๋ชฌ

์ฃผ์š” ๊ฐœ๋… | Recoil

0๊ฐœ์˜ ๋Œ“๊ธ€