Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ ๐ŸŒˆ Notice ] Recoil ์‚ฌ์šฉ๋ฒ• #2

Closed
Happhee opened this issue Jan 1, 2023 · 0 comments
Closed

[ ๐ŸŒˆ Notice ] Recoil ์‚ฌ์šฉ๋ฒ• #2

Happhee opened this issue Jan 1, 2023 · 0 comments
Assignees

Comments

@Happhee
Copy link
Contributor

Happhee commented Jan 1, 2023

โœจ Recoil ์ด๋ž€?

image

Recoil์˜ ๊ธฐ๋ณธ, โš›๏ธ Atom

๊ณต์‹๋ฌธ์„œ์—์„œ์˜ atom์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
atoms (๊ณต์œ  ์ƒํƒœ) ์€ ๊ธฐ์กด์˜ redux์—์„œ ์“ฐ์ด๋Š” store ์™€ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

atom์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฉด, ํ•ด๋‹น atom์„ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋˜ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋“ค์˜ state๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋ฆฌ๋ Œ๋” ๋œ๋‹ค !

unique ํ•œ id์ธ key๋กœ ๊ตฌ๋ถ„๋˜๋Š” ๊ฐ atom์€, ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ atom์„ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ทธ ์ปดํฌ๋„ŒํŠธ๋“ค๋„ ๋˜‘๊ฐ™์€ ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹คโœจ
(์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ฐ”๋€ ๊ฐ’์œผ๋กœ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋“ค์ด re-render )

๐Ÿ‘‡ ์ž์„ธํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ์—!
[Recoil] Recoil 200% ํ™œ์šฉํ•˜๊ธฐ

โœจ Pic.me์— ์ ์šฉํ•˜๊ธฐ!

๐Ÿ“‚ ํด๋”๊ตฌ์กฐ

image

์šฐ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์ด ๋ฉ”์ด์ปค์™€ ํ”Œ๋ ˆ์ด์–ด๋กœ ๋‚˜๋‰˜์–ด ์žˆ๊ธฐ์— recoil ํด๋” ์•ˆ์— maker, player ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ์•ˆ์—์„œ ๋‹ค์‹œ atom, selector ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋†“์•˜์Šต๋‹ˆ๋‹ค.

โœ… RecoilRoot

RecoilRoot๋Š” ๋ฆฌ๋•์Šค์˜ Provier์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์—ฌ๋Ÿฌ๊ฐœ์˜ RecoilRoot๊ฐ€ ๊ณต์กดํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ atom state์˜ ๋…๋ฆฝ์ ์ธ ๊ณต๊ธ‰์ž / ์ €์žฅ์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค!

๐Ÿ“„ components/Router.tsx

const Router = () => (
  <BrowserRouter>
    <RecoilRoot>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/library" element={<Library />} />
        <Route path="/login" element={<Login />} />
        <Route path="/signup" element={<Signup />} />
        <Route path="/playerlanding" element={<PlayerLanding />} />
        <Route path="/makerlanding" element={<MakerLanding />} />
        <Route path="/voting" element={<Voting />} />
        <Route path="*" element={<Error404 />} />
      </Routes>
    </RecoilRoot>
  </BrowserRouter>
);

โœ… atom

atom์€ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์ž *์ „์—ญ ์ €์žฅ์†Œ**๋ฅผ ๋งŒ๋“œ๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก Tip) api๋ช…์„ธ์„œ์— ์ ํžŒ ๋ฐ์ดํ„ฐ ๋„ค์ด๋ฐ์„ ์‚ฌ์šฉํ•ด ์ƒํƒœ๋ช…์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

๐Ÿ“„ recoil/player/atom.ts

import { atom } from 'recoil';
import { recoilPersist } from 'recoil-persist';

import { VotingInfo } from './../../types/voting';

// โœ…  ํŽ˜์ด์ง€๊ฐ€ ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ์œ ์ง€
const { persistAtom } = recoilPersist();

// โœ… unique ID ๋ฅผ ๊ฐ€์ง€๊ณ  ์ดˆ๊ธฐ ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!
export const votingInfoState = atom<VotingInfo>({
  key: 'votingInfo',
  default: {
    vote_id: 0,
    user_id: 0,
    vote_status: false,
    vote_title: '',
    pictures: [],
    current_vote: 0,
    maximum_vote: 0,
    created_date: new Date(0),
  },
  effects_UNSTABLE: [persistAtom],
});

โœ… selector

selector๋Š” recoil์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ์˜ ํŠน์ • ๋ถ€๋ถ„๋งŒ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ํ•ด๋‹น ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ์‚ฐํ•œ ๊ฐ’์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹ค๋ฅธ atom์— ์˜์กดํ•˜๋Š” ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํŠน์ง•์ž…๋‹ˆ๋‹ค.
๋”๋ถˆ์–ด ๋™์ผํ•œ Parameter๋ฅผ ์ œ๊ณตํ•˜๋ฉด ๋™์ผํ•œ ๊ฐ’์„ return ํ•˜๋Š” ์ˆœ์ˆ˜ํ•จ์ˆ˜์˜ ๊ฐœ๋…์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“„ recoil/player/selector.ts

import { selector } from 'recoil';

import { votingList } from './atom';

export const votingIdStateSelector = selectorFamily<VotingInfo[], number>(({
  key: 'votingIdSelector',

  get:  (param : number) => ({ get }) => get(votingList).filter((votingInfo)=> votingInfo.vote_id === number),
});

โœ… ์ปดํฌ๋„ŒํŠธ or ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

  • useRecoilState ๐Ÿ‘‰ atom์—์„œ ์ •์˜ํ•œ ์ „์—ญ์ƒํƒœ( ์—ฌ๊ธฐ์„œ๋Š” userInfoState)๋ฅผ state์ฒ˜๋Ÿผ ๊ด€๋ฆฌ
  • useSetRecoilState ๐Ÿ‘‰ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” setter ํ•จ์ˆ˜๋งŒ ๋ฐ˜ํ™˜
  • useRecoilValue ๐Ÿ‘‰ setter ํ•จ์ˆ˜ ์—†์ด atom์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜๋งŒ ํ•œ๋‹ค.
  • useResetRecoilState ๐Ÿ‘‰ atom์—์„œ ์ •์˜ํ•œ ์ „์—ญ์ƒํƒœ ์ดˆ๊ธฐํ™”

๐Ÿ“„ pages/Voting.tsx

// state ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค!
const [newVotingInfo, setNewVotingInfo] = useRecoilState(votingStateSelector);

// ๐Ÿ‘‡ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์…€๋ ‰ํ„ฐ ์‚ฌ์šฉ๋ฒ•
// useRecoilValue(votingIdStateSelector(voite_id));
  useEffect(() => {
    if (votingInfo) {
      setNewVotingInfo(votingInfo.data);
    }
  }, []);

๐Ÿ“š Reference


๐ŸŒผ ์งˆ๋ฌธ ๋„ˆ๋ฌด๋„ˆ๋ฌด ์กฐ์•„์š” ์•„์Ÿˆ์•„์Ÿˆ ๐ŸŒผ

@Happhee Happhee self-assigned this Jan 1, 2023
@Happhee Happhee pinned this issue Jan 1, 2023
@Happhee Happhee closed this as completed Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant