Skip to content

Redux vs Recoil

Youngho Kim edited this page Nov 23, 2022 · 2 revisions
  • ์ž‘์„ฑ์ž: J201_์ตœ๋ช…์„ฑ

์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Redux์™€ Recoil์„ ๋น„๊ตํ•˜๊ณ  ๊ฒฐ๋ก ์ ์œผ๋กœ Redux๊ฐ€ ์•„๋‹Œ Recoil์„ ๊ณ ๋ฅธ ์ด์œ ์— ๋Œ€ํ•œ ๊ธ€.

Redux

์žฅ์ 

๋งŽ์€ ์ƒํƒœ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์ข‹๋‹ค.

์ƒํƒœ๊ฐ’์ด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ ๋””๋ฒ„๊น…์ด ์ƒ๋Œ€์ ์œผ๋กœ Recoil์— ๋น„ํ•ด ๋” ํŽธํ•ด์งˆ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

Redux Devtool

Redux Devtools๋ฅผ ์ด์šฉํ•ด ์ง๊ด€์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ๋˜ํ•œ ์ œ๊ณตํ•œ๋‹ค.

๋‹จ์ 

์œ ์ง€ ๋ณด์ˆ˜๊ฐ€ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

์ž‘์€ ์ƒํƒœ ํ•˜๋‚˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉดย actions,ย reducer,ย type ๋“ฑ ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๋‹ค.

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋„ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

๋‚ด์žฅ ๊ธฐ๋Šฅ์ด ์•„๋‹Œ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•ด์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

redux-thunkย ํ˜น์€ย redux-saga,ย redux-toolkit๋“ฑ์„ ์ด์šฉ

Recoil

์žฅ์ 

์ง๊ด€์ ์ด๋ฉด์„œ ๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ

React์—์„œ ์‚ฌ์šฉํ•˜๋Š” Hook๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์„œ ๊ตฌ์กฐ๊ฐ€ ๊ฐ„๋‹จํ•˜์—ฌ ์ฝ”๋“œ ์–‘์ด ์ค„์–ด๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์ˆœํžˆ RecoilRoot๋กœ ๊ฐ์‹ธ๊ณ  atom๋‹จ์œ„๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Ÿฌ๋‹์ปค๋ธŒ๊ฐ€ ๋‚ฎ๋‹ค.

const myRecoilState = atom({
	key: "",
	default: "",
})

const [state, setState] = useReocilState(myRecoilState)

์‰ฌ์šด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

Selector๋ผ๋Š” ๋‚ด์žฅ๋œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

// https://recoiljs.org/docs/guides/asynchronous-data-queries
const currentUserNameQuery = selector({
  key: 'CurrentUserName',
  get: async ({get}) => {
    const response = await myDBQuery({
      userID: get(currentUserIDState),
    });
    return response.name;
  },
});

๋‹จ์ 

๊ฐœ๋ฐœ์ž ๋„๊ตฌ๊ฐ€ ์™„๋ฒฝํ•˜์ง€ ์•Š๋‹ค.

๋””๋ฒ„๊น… ๋ฐ ์Šค๋ƒ…์ƒท ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋Š”๋ฐ ์žˆ์–ด ์‹ ๋ขฐ์„ฑ์ด ๋ถ€์กฑํ•˜๋‹ค.

Recoilize๋ผ๋Š” Devtool์ด ์žˆ๋Š”๋ฐ ์ž‘์€ ๋ฒ„๊ทธ๋“ค์ด ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  API๋“ค์ด ๋†’์€ ์‹ ๋ขฐ์„ฑ์„ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

useGetRecoilValue, useRecoilRefresher ๋“ฑ์€ ๊ณต์‹๋ฌธ์„œ๋„ UNSTABLE๋กœ ๋ถ„๋ฅ˜.

๊ฒฐ๋ก 

ํ”„๋กœ์ ํŠธ ํฌ๊ธฐ๋ฅผ ๊ณ ๋ คํ–ˆ์„ ๋•Œ ๊ทธ๋ ‡๊ฒŒ ๋งŽ์€ ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค. Recoil์ด ๋””๋ฒ„๊น…์ด ๋ถˆํŽธํ•˜๊ธด ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํฐ ํ”„๋กœ์ ํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋””๋ฒ„๊น… ๋ณด๋‹ค๋Š” ์งง์€ ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„ ๋™์•ˆ ์œ ์ง€ ๋ณด์ˆ˜์™€ ๋Ÿฌ๋‹์ปค๋ธŒ๋ฅผ ๊ณ ๋ ค ํ–ˆ์„ ๋•Œ Redux๋Š” ๋„ˆ๋ฌด ๋ฌด๊ฒ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— Recoil์„ ๊ธฐ์ˆ ์Šคํƒ์œผ๋กœ ์ถ”์ฒœํ–ˆ๋‹ค.

์ฐธ์กฐ

Redux ์‹œ์ž‘ํ•˜๊ธฐ | Redux

Recoil ์—์„œ Selector ๊ธฐ๋Šฅ get, set ์‚ฌ์šฉ๋ฒ•

Installation | Recoil

๐Ÿ“š ๊ทธ๋ผ์šด๋“œ ๋ฃฐ

โœ๏ธ ์ปจ๋ฒค์…˜

๐Ÿง‘โ€๐Ÿซ ๋ฉ˜ํ† ๋ง

๐Ÿ“ ์• ์ž์ผ ํ”„๋กœ์„ธ์Šค

๊ธฐํš
๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ
์Šคํ”„๋ฆฐํŠธ ๋ฆฌ๋ทฐ
์Šคํ”„๋ฆฐํŠธ ํšŒ๊ณ 
ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…
๊ธฐํƒ€ ์‚ฐ์ถœ๋ฌผ

๐Ÿ“– ๊ธฐ์ˆ ๋ฌธ์„œ

Week2
Week3
Week4
Week5

๐Ÿ—‚ ์ฐธ๊ณ ๋ฌธ์„œ

Clone this wiki locally