DevYoon

[Recoil] Recoil์ด๋ž€? ๋ณธ๋ฌธ

Web/React.js

[Recoil] Recoil์ด๋ž€?

gimewn 2022. 9. 13. 17:07

๐Ÿ’พ Recoil

1๏ธโƒฃ Recoil์ด๋ž€?

React์˜ hooks์™€ ์–ด์šธ๋ฆฌ๋ฉด์„œ, React์— ํ•„์š”ํ•œ ๊ฒƒ๋“ค๋งŒ React์Šค๋Ÿฝ๊ฒŒ ์ œ์ž‘ํ•œ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๊ธฐ์กด์˜ Redux๋‚˜ Mobx์™€ ๊ฐ™์€ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ React ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹˜

  • store๋Š” ์™ธ๋ถ€์š”์ธ์œผ๋กœ ์ทจ๊ธ‰๋˜๋ฏ€๋กœ, React์˜ ๋‚ด๋ถ€ ์Šค์ผ€์ค„๋Ÿฌ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Œ

    • ๋™์‹œ์„ฑ ๋ชจ๋“œ์˜ ๋“ฑ์žฅ์œผ๋กœ ์œ„ ์‚ฌ์‹ค์ด ์ค‘์š”ํ•ด์ง

      • ๋™์‹œ์„ฑ ๋ชจ๋“œ โ“

        ํ”„๋กœ๊ทธ๋žจ์„ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ตฌ์กฐํ™”

        โžก๏ธ ๋‹จ์ผ ์Šค๋ ˆ๋“œ ์‚ฌ์šฉ์˜ ํ•œ๊ณ„๋ฅผ ๊นจ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“ฆ

        ์ฐธ๊ณ  ๐Ÿ”— https://medium.com/swlh/what-is-react-concurrent-mode-46989b5f15da

  • ๊ธฐ๋ณธ์ ์ธ store ๊ตฌ์„ฑ์„ ์œ„ํ•ด ๋งŽ์€ ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ์™€ ์žฅํ™ฉํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•จ

  • ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋˜๋Š” ๊ณ„์‚ฐ๋œ ๊ฐ’ ์บ์‹œ์™€ ๊ฐ™์€ ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

2๏ธโƒฃ ์„ค์น˜

// npm ์‚ฌ์šฉ ์‹œ
npm install recoil
// yarn ์‚ฌ์šฉ ์‹œ
yarn add recoil

3๏ธโƒฃ recoilRoot

import React from 'react';
import {
    RecoilRoot,
    atom,
    selector,
    useRecoilState,
    useRecoilValue,
} from 'recoil';

function App() {
    return (
        <RecoilRoot>
            <CharacterCounter />
        </RecoilRoot>
    );
}
  • recoil state๋ฅผ ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ๋ฅผ RecoilRoot ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•จ

4๏ธโƒฃ Atom

์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋Š” React state, ํ•˜๋‚˜์˜ ์ƒํƒœ

  • atom์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๊ทธ๊ฒƒ์„ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๋ชจ๋‘ ๋‹ค์‹œ ๋ Œ๋”๋ง๋จ
  • ์•„ํ†ฐ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ณ ์œ ํ•œ ํ‚ค ๊ฐ’๊ณผ ๋””ํดํŠธ ๊ฐ’์„ ์„ค์ •ํ•ด์•ผ ํ•จ
  • ๋””ํดํŠธ ๊ฐ’์€ ์ •์ ์ธ ๊ฐ’, ํ•จ์ˆ˜, ๋น„๋™๊ธฐ ํ•จ์ˆ˜(์ถ”ํ›„ ์ง€์›) ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ๋‹ค
export const nameState = atom({
  key: 'nameState',
  default: 'Yoonju Kim'
});

useRecoilState

atom์˜ ๊ฐ’์„ ๊ตฌ๋…ํ•˜์—ฌ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋Š” hook์œผ๋กœ, useState์™€ ๋™์ผํ•œ ๋ฐฉ์‹

useRecoilValue

setter ํ•จ์ˆ˜ ์—†์ด atom์˜ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•จ

useSetRecoilState

setter ํ•จ์ˆ˜๋งŒ ๋ฐ˜ํ™˜ํ•จ

import {nameState} from './someplace'
// useRecoilState
const NameInput = () => {
  const [name, setName] = useRecoilState(nameState);
  const onChange = (event) => {
   setName(event.target.value);
  };
  return <>
   <input type="text" value={name} onChange={onChange} />
   <div>Name: {name}</div>
  </>;
}
// useRecoilValue
const SomeOtherComponentWithName = () => {
  const name = useRecoilValue(nameState);
  return <div>{name}</div>;
}
// useSetRecoilState  
const SomeOtherComponentThatSetsName = () => {
  const setName = useSetRecoilState(nameState);
  return <button onClick={() => setName('Jon Doe')}>Set Name</button>;
}

5๏ธโƒฃ Selector

์ƒํƒœ์—์„œ ํŒŒ์ƒ๋œ ๋ฐ์ดํ„ฐ๋กœ, ๋‹ค๋ฅธ atom์— ์˜์กดํ•˜๋Š” ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ

  • get
  • set : ํ•˜๋‚˜ ์ด์ƒ์˜ atom์„ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Œ
// ๋™๋ฌผ ๋ชฉ๋ก ์ƒํƒœ
const animalsState = atom({
  key: 'animalsState',
  default: [{
    name: 'Rexy',
    type: 'Dog'
  }, {
    name: 'Oscar',
    type: 'Cat'
  }],
});
// ํ•„ํ„ฐ๋ง ๋™๋ฌผ ์ƒํƒœ
const animalFilterState = atom({
 key: 'animalFilterState',
 default: 'dog',
});
// ํŒŒ์ƒ๋œ ๋™๋ฌผ ํ•„ํ„ฐ๋ง ๋ชฉ๋ก
const filteredAnimalsState = selector({
 key: 'animalListState',
 get: ({get}) => {
   const filter = get(animalFilterState);
   const animals = get(animalsState);

   return animals.filter(animal => animal.type === filter);
 }
});
// ํ•„ํ„ฐ๋ง๋œ ๋™๋ฌผ ๋ชฉ๋ก์„ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ
const Animals = () => {
  const animals = useRecoilValue(filteredAnimalsState);
  return animals.map(animal => (<div>{ animal.name }, { animal.type    }</div>));
}

์ฐธ๊ณ  ๐Ÿ”— https://codesandbox.io/s/hik7u?file=/src/App.js

โœ ์ฐธ๊ณ  ๊ธ€ ๋ชฉ๋ก

Recoil ๊ณต์‹ ๋ฌธ์„œ ๐Ÿ”— https://recoiljs.org/ko/docs/introduction/installation

Recoil - ๋˜ ๋‹ค๋ฅธ React ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ? ๐Ÿ”— https://ui.toast.com/weekly-pick/ko_20200616#%EC%B6%94%EA%B0%80-%EC%9D%BD%EC%9D%84-%EA%B1%B0%EB%A6%AC

React์Šค๋Ÿฌ์šด ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ :: Recoil ๐Ÿ”— https://velog.io/@juno7803/React%EC%8A%A4%EB%9F%AC%EC%9A%B4-%EC%83%81%ED%83%9C%EA%B4%80%EB%A6%AC-%EB%9D%BC%EC%9D%B4%EB%B8%8C%EB%9F%AC%EB%A6%AC-Recoil

Recoil 200% ํ™œ์šฉํ•˜๊ธฐ ๐Ÿ”— https://velog.io/@juno7803/Recoil-Recoil-200-%ED%99%9C%EC%9A%A9%ED%95%98%EA%B8%B0

What is React Concurrent Mode? ๐Ÿ”— https://medium.com/swlh/what-is-react-concurrent-mode-46989b5f15da