new TIL. Recoil

์œ ์žํƒฑ์ž๐Ÿ‹ยท2021๋…„ 4์›” 13์ผ
0
post-thumbnail

Redux vs. Mobx, ์น˜์—ดํ•œ ๋…ผ์Ÿ์—์„œ ์ƒˆ๋กญ๊ฒŒ ๋“ฑ์žฅํ•œ Recoil.
Recoil์€ ๋ฌด์—‡์ผ๊นŒ?


๐Ÿ“Œ Recoil?

"A state management library for React"


๐Ÿ“Œ RecoilRoot

import React from 'react';
import {
  RecoilRoot,
  atom,
  selector,
  useRecoilState,
  useRecoilValue,
} from 'recoil';
ใ…ค
function App() {
  return (
    <RecoilRoot>
      <CharacterCounter />
    </RecoilRoot>
  );
}

Components that use recoil state need RecoilRoot to appear somewhere in the parent tree.


๐Ÿ“Œ Atom - "๋ฐ์ดํ„ฐ ์กฐ๊ฐ"

Atom

const fontSizeState = atom({
  key: 'fontSizeState',
  default: 14,
});

Atoms are units of state. They're updateable and subscribable: when an atom is updated, each subscribed component is re-rendered with the new value. They can be created at runtime, too. Atoms can be used in place of React local component state. If the same atom is used from multiple components, all those components share their state.


๐Ÿ“Œ useRecoilState

useRecoilState

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

To read and write an atom from a component, we use a hook called useRecoilState. It's just like React's useState, but now the state can be shared between components.


๐Ÿ“Œ selector

1. Atom์—์„œ ํŒŒ์ƒ๋œ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ
2. ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ˆœ์ˆ˜ํ•จ์ˆ˜

selector

const fontSizeLabelState = selector({
  key: 'fontSizeLabelState',
  get: ({get}) => {
    const fontSize = get(fontSizeState);
    const unit = 'px';
ใ…ค
    return `${fontSize}${unit}`;
  },
});

selector๋Š” atom์˜ ์ƒํƒœ์— ์˜์กดํ•˜๋Š” ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. selector์—์„œ๋Š” get ํ•จ์ˆ˜(ํ•„์ˆ˜ํ•ญ๋ชฉ)๋ฅผ ํ†ตํ•ด atom ์ •๋ณด๋“ค์„ 1๊ฐœ ์ด์ƒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด atom์„ ์กฐํ•ฉํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌผ๋ก  atom์˜ ์ •๋ณด๊ฐ€ ๋ฐ”๋€Œ๋ฉด ํ•ด๋‹น atom์„ ์˜์กดํ•˜๋Š” selector๋„ ์ž๋™์œผ๋กœ ๋ฆฌ๋žœ๋”๋ง์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•œ๊ฐœ ์ด์ƒ์˜ atom ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋„๋ก set ํ•จ์ˆ˜(์„ ํƒํ•ญ๋ชฉ)๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
[์ธ์šฉ_https://blog.woolta.com/categories/1/posts/209]


๐Ÿ“Œ useRecoilValue

useRecoilValue

function FontButton() {
  const [fontSize, setFontSize] = useRecoilState(fontSizeState);
  const fontSizeLabel = useRecoilValue(fontSizeLabelState);
ใ…ค
  return (
    <>
      <div>Current font size: {fontSizeLabel}</div>
      <button onClick={() => setFontSize(fontSize + 1)} style={{fontSize}}>
        Click to Enlarge
      </button>
    </>
  );
}

๊ตฌ๋…ํ•˜๋Š” ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ฐ’์˜ update ํ•จ์ˆ˜๊ฐ€ ํ•„์š”์—†์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ Suspense

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์™„์ „ํžˆ ๋žœ๋”๋ง๋  ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๋žœ๋”๋ง์„ ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋‹ค๋ฆฌ๋Š” ์—ญํ• 
  • ๊ฐ„๋‹จํ•˜๊ฒŒ ๋น„๋™๊ธฐ ์ƒํƒœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

๐Ÿ“Œ Concurrent Mode

  • ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์–ธ์ œ ๋žœ๋”๋งํ• ์ง€ ๋ฆฌ์•กํŠธ framwork๊ฐ€ ์•Œ์•„์„œ ์ตœ์ ํ™”๋ฅผ ํ•ด์ฃผ๋Š” ๊ฐœ๋…
  • ๊ฐ„๋‹จํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” API๊ฐ€ ๋ฐ”๋กœ React Suspense


[์ฐธ๊ณ ]

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