โœ๐Ÿป [Code Camp_TIL] 22์ผ์ฐจ: global state, Recoil

code_Jยท2023๋…„ 4์›” 22์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
28/41
post-thumbnail

global state

ํ•˜๋‚˜์˜ state๊ฐ€ ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€์—์„œ ํ•„์š”ํ•  ๋•Œ, global state์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ , ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰ global state๋Š” ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” state์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด store์— name์ด๋ผ๋Š” state๋ฅผ ์ €์žฅํ•˜๊ณ  ํ•„์š”ํ•œ ์ปดํฌ๋„ŒํŠธ์—์„œ importํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒˆ๊ฑฐ๋กญ๊ฒŒ props drilling์„ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

global state์˜ ํˆด๋กœ๋Š” context-Api, Redux, Recoil ๋“ฑ์ด ์žˆ๋‹ค.

global state tool: Redux -> MobX -> SWR -> (Rest api)react-query / (graphql api)apollo-client + Recoil


fetch policy

apollo-client๋กœ global state๋ฅผ ๋งŒ๋“ค๋ฉด Apollo-Cache์— ์ €์žฅ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ๊ฒฝ์šฐ, Apollo-Cache์— ๊ฐ€์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์—†์œผ๋ฉด ๋ฐฑ์—”๋“œ์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ๋ฐฑ์—”๋“œ์— ์š”์ฒญํ•˜์ง€ ์•Š๊ณ  Apollo-Cache์—์„œ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ”๋กœ ๋ณด๋‚ด์ค€๋‹ค.

์ด๊ฒƒ์„ Apollo-client์˜ fetchPolicy(fetch ์ •์ฑ…)์ด๋ผ๊ณ  ํ•œ๋‹ค.

fetch policy์—๋Š” ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉฐ, ์ƒํ™ฉ์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

cache-first(default): ์บ์‹œ์— ์žˆ๋Š”์ง€ ๋จผ์ € ํ™•์ธ
network-only: ์บ์‹œ์— ์žˆ๋Š”์ง€์— ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ๋ฐฑ์—”๋“œ์— ์š”์ฒญ


Recoil

์ตœ๊ทผ์— ๋‚˜์˜จ ํˆด์€ ์ž๋™ํ™”๊ฐ€ ๋งŽ์ด ๋˜์–ด์žˆ๋‹ค! ์š”์ฆ˜์€ ์„œ๋ฒ„๋ฐ์ดํ„ฐ ์บ์‹ฑ์œผ๋กœ๋Š” react-query์™€ apollo-client๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ , ๋กœ์ปฌ๋ฐ์ดํ„ฐ ์บ์‹ฑ ์šฉ๋„๋กœ๋Š” Recoil์„ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด๋‹ค.

Recoil์€ ๋ฏธ๋‹ˆ Redux๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Redux๋„ ์ด์— ์งˆ์„ธ๋ผ ์ตœ๊ทผ์—๋Š” Redux-toolkit๊ณผ, Redux-ToolKit-Query(RTK Query)๋ฅผ ๋‚ด๋†“์œผ๋ฉด์„œ ์‚ฌ์šฉ์˜ ํŽธ๋ฆฌ์„ฑ์„ ๋†’์˜€๋‹ค.

์•„๋ฌดํŠผ, ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Recoil์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ํ”„๋ก ํŠธ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•ด๋ดค์ž ๋งŽ์€ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜์ง€ ์•Š์„ ํ…๋ฐ Redux๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ๋„ˆ๋ฌด ํ—ค๋น„ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


Recoil ์„ค์น˜

npm install recoil
yarn add recoil


Recoil setting

//app.tsx ํŒŒ์ผ 
import {
  RecoilRoot,
  atom,
  selector,
  useRecoilState,
  useRecoilValue,
} from 'recoil';

function App() {
  return (
    <RecoilRoot>
		//RecoilRoot๋กœ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ ๋ฌถ์–ด์ฃผ๊ธฐ
      <Component />
    </RecoilRoot>
  );
}

Recoil ์‚ฌ์šฉ

Recoil์—์„œ๋Š” Atom์œผ๋กœ state์˜ ์ผ๋ถ€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ปดํฌ๋„ŒํŠธ๋“ค์€ ์ž์‹ ์ด ํ•„์š”ํ•œ Atom์„ ์ฐธ์กฐํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ž์‹ ์ด ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” Atom์— ๋ณ€ํ™”๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น atom์„ ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฆฌ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚œ๋‹ค.

// Atom
const textState = atom({
  key: 'textState', // state์˜ ์ด๋ฆ„
  default: '', //์ดˆ๊ธฐ๊ฐ’
});

Atom์„ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  useRecoilState๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

// TextInput ์ปดํฌ๋„ŒํŠธ 
function TextInput() {
  const [text, setText] = useRecoilState(textState);

  const onChange = (event) => {
    setText(event.target.value);
  };

  return (
    <div>
      <input type="text" value={text} onChange={onChange} />
      <br />
      Echo: {text}
    </div>
  );
}

์œ„์™€ ๊ฐ™์ด ์ ์–ด์ฃผ๋ฉด, textState๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฆฌ๋ Œ๋”๊ฐ€ ์ผ์–ด๋‚œ๋‹ค.



profile
Web FE ๊ฐœ๋ฐœ์ž ์ทจ์ค€์ƒ

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