๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Front
Recoil
Recoil

1. Recoil ์ด๋ž€?

  • React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • Facebook (๋ฉ”ํƒ€)์—์„œ ๊ฐœ๋ฐœํ•œ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, Atom ์ด๋ผ๋Š” ๋‹จ์œ„๋กœ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ ์ด์šฉํ•ด ์ปดํฌ๋„ŒํŠธ ์‚ฌ์ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๋ฉฐ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•จ
  • Recoil ๊ณต์‹๋ฌธ์„œ https://recoiljs.org/ko/

1.1 ์™œ Recoil๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

  • ๊ฐ„ํŽธํ•œ ์ƒํƒœ๊ด€๋ฆฌ: ๊ฐ„ํŽธํ•˜๊ฒŒ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ (์ „์—ญ ์ƒํƒœ)
  • ์ตœ์ ํ™”๋œ ๋ฆฌ๋ Œ๋”๋ง: ๋‚ด๋ถ€์ ์œผ๋กœ ์ตœ์ ํ™”๋˜์–ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ๋ฆฌ๋ Œ๋”๋ง
  • ๋ณต์žกํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ํ•ฉ: ๋ณต์žกํ•œ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๋‹ค๋ฃจ๋Š”๋ฐ ์ ํ•ฉํ•จ

1.2 ์„ค์น˜ ๋ฐ ์„ธํŒ…

cf. https://recoiljs.org/ko/docs/introduction/installation

(1) ์„ค์น˜

1
yarn add recoil

(2) ๋ฃจํŠธ ์ปดํฌ๋„ŒํŠธ์— RecoilRoot ์„ธํŒ…

1
import React from 'react'
2
import { RecoilRoot } from 'recoil'
3
4
function App() {
5
return (
6
<RecoilRoot>
7
<div>์•ฑ ๋‚ด์šฉ</div>
8
</RecoilRoot>
9
)
10
}

(3) Atom ๋ฐ Selector ํ•จ์ˆ˜ ์ •์˜

1
export const counterState = atom({
2
key: 'counterState', // ๊ณ ์œ ํ•œ ํ‚ค๊ฐ’
3
default: 0, // ์ดˆ๊ธฐ๊ฐ’
4
})

2. Recoil ๊ธฐ๋Šฅ

2.1 Atom

1
import { atom } from 'recoil'
2
3
export const counterState = atom({
4
key: 'counterState', // ๊ณ ์œ ํ•œ ํ‚ค๊ฐ’
5
default: 0, // ์ดˆ๊ธฐ๊ฐ’
6
})
  • Atom: Recoil์—์„œ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์ผ ๋‹จ์ˆœํ•œ ์ƒํƒœ ์กฐ๊ฐ
  • ์ „์—ญ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ
  • ํ•ด๋‹น ์ƒํƒœ๋ฅผ ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ์Œ

2.2 Selector

1
import { Selector } from 'recoil'
2
import { counterState } from './atoms'
3
4
export const squaredCounterState = selector({
5
key: 'squaredCounterState', // ๊ณ ์œ ํ•œ ํ‚ค๊ฐ’
6
get: ({ get }) => {
7
const counter = get(counterState)
8
return counter ** 2
9
},
10
})
  • Selector: Recoil์—์„œ ํŒŒ์ƒ๋œ ์ƒํƒœ
  • ๋‹ค๋ฅธ atom ๋˜๋Š” selector์˜ ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ์ œ๊ณต
  • ๋น„๋™๊ธฐ ์ž‘์—…์ด๋‚˜ ๋ณต์žกํ•œ ๊ณ„์‚ฐ์— ์‚ฌ์šฉ

3. Recoil Hooks

3.1 useRecoilState

  • atom ๋˜๋Š” selector์˜ ์ƒํƒœ์™€ ํ•ด๋‹น ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
1
import React from 'react'
2
import { useRecoilState } from 'recoil'
3
import { counterState } from './atoms'
4
5
export default function Counter() {
6
// counterState๋ฅผ ์‚ฌ์šฉํ•ด ์ „์—ญ ์ƒํƒœ๊ฐ’ counter๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉ
7
const [counter, setCounter] = useRecoilState(counterState)
8
9
const increment = () => setCounter(counter + 1)
10
const decrement = () => setCounter(counter - 1)
11
12
return (
13
<div>
14
<h1>์นด์šดํŠธ : {counter}</h1>
15
<button onClick={increment}>์ฆ๊ฐ€</button>
16
<button onClick={decrement}>๊ฐ์†Œ</button>
17
</div>
18
)
19
}

3.2 useRecoilValue

  • atom ๋˜๋Š” selector์˜ ํ˜„์žฌ ์ƒํƒœ ๊ฐ’์„ ๋ฐ˜ํ™˜
1
import React from 'react'
2
import { useRecoilValue } from 'recoil'
3
import { squardCounterState } from './selectors'
4
5
export default function SquaredCounter() {
6
// useRecoilValue ํ›…์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š” selector ๊ฐ’ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ
7
const squaredCounter = useRecoilValue(squardCounterState)
8
return <p>์ œ๊ณฑ ์นด์šดํŠธ ๊ฐ’ : {counter}</p>
9
}

3.3 useSetRecoilState

  • atom ๋˜๋Š” selector์˜ ํ˜„์žฌ ์ƒํƒœ ๊ฐ’์„ ๋ฐ˜ํ™˜
1
import React from 'react'
2
import { useRecoilValue } from 'recoil'
3
import { counterState } from './atoms'
4
5
export default function ResetCounter() {
6
// useSetRecoilState ํ›…์œผ๋กœ ์นด์šดํ„ฐ ๊ฐ’์„ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ (setter ํ•จ์ˆ˜)
7
const setCounter = useSetRecoilState(counterState)
8
const reset = () => setCounter(0)
9
return <button onClick={reset}>์ดˆ๊ธฐํ™”</button>
10
}

3.4 useResetRecoilState

  • atom ๋˜๋Š” selector์˜ ํ˜„์žฌ ์ƒํƒœ ๊ฐ’์„ ๋ฐ˜ํ™˜
1
import React from 'react'
2
import { useResetRecoilState } from 'recoil'
3
import { counterState } from './atoms'
4
5
export default function ResetCounter() {
6
// useResetRecoilState ํ›…์œผ๋กœ counter ๊ฐ’ ์ดˆ๊ธฐํ™”
7
const resetCounter = useResetRecoilState(counterState)
8
const reset = () => resetCounter()
9
return <button onClick={reset}>์ดˆ๊ธฐํ™”</button>
10
}

4. ์ฐธ๊ณ