[React] useReducer()

Hyunยท2022๋…„ 1์›” 10์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
19/22
post-thumbnail

๐Ÿ’กuseReducer()๋ž€?

์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ๋ณ€ํ™” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์„๊ฐ€์ง„ React HOOK

์‚ฌ์šฉ์ž ๋ฆฌ์ŠคํŠธ ๊ธฐ๋Šฅ์—์„œ์˜ ์ฃผ์š” ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง์€ App ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์ด๋ฃจ์–ด์ง€๋„๋ก ๋งŒ๋“ค์—ˆ๊ณ , ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ์—๋Š” useState๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ๋กœ์šด์ƒํƒœ๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.
์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ฒŒ๋  ๋•Œ useState๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ ๋ง๊ณ ๋„ useReducer๊ฐ€ ์žˆ๋‹ค.

์ด Hook ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง์„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถ„๋ฆฌ์‹œํ‚ฌ์ˆ˜ ์žˆ์–ด์„œ
1)์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง์„ ์ปดํฌ๋„ŒํŠธ ๋ฐ”๊นฅ์— ์ž‘์„ฑ ํ•  ์ˆ˜๋„ ์žˆ๊ณ ,
2)์‹ฌ์ง€์–ด ๋‹ค๋ฅธ ํŒŒ์ผ์— ์ž‘์„ฑ ํ›„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๐ŸงuseState VS useReducer

<useState>


useState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์งฐ์„๊ฒฝ์šฐ

<useReducer>


useReducer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์งฐ์„ ๊ฒฝ์šฐ
์™ผ์ชฝ์— ๋ณด์ด๋Š” reducer(์ƒํƒœ๋ณ€ํ™”ํ•จ์ˆ˜)๋ฅผ ์ปดํฌ๋„ŒํŠธ ๋ฐ”๊นฅ์œผ๋กœ ๋ถ„๋ฆฌ๋ฅผํ•ด์„œ ๋‹ค์–‘ํ•œ ์ƒํƒœ๋ณ€ํ™”๋กœ์ง์„ ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์Šค์œ„์น˜์ผ€์ด์Šค๋ฌธ๋ฒ•์ฒ˜๋Ÿผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊ฟ€์ˆ˜์žˆ๋‹ค.

์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ๋ณด๋ฉด
const Counter = () => {
๋น„๊ตฌ์กฐํ™”ํ• ๋‹น์„ ํ†ตํ•ด์„œ ์‚ฌ์šฉ์„ํ•˜๊ณ ,

const [count, dispatch]
useState๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ์ด ๋ฐฐ์—ด[]์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ
์ฒซ๋ฒˆ์งธ๋กœ ๋ฐ˜ํ™˜๋ฐ›๊ฒŒ๋˜๋Š” 0๋ฒˆ์งธ์ธ๋ฑ์Šค๋กœ๋Š” ํ˜„์žฌ์ƒํƒœ(state),
๋‘๋ฒˆ์งธ๋กœ ๋ฐ˜ํ™˜๋ฐ›๊ฒŒ๋˜๋Š” 1๋ฒˆ์งธ์ธ๋ฑ์Šค๋กœ๋Š” ์ƒํƒœ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๋Š”ํ•จ์ˆ˜๋ฅผ ๋„ฃ๋Š”๋‹ค.

useReducer(reducer, 1);
๊ทธ๋ฆฌ๊ณ  useReducer๋ฅผ ์‚ฌ์šฉํ• ๋•Œ
์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๊ผญ reducer๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•œ๋‹ค.
์•ž์„œ ์ฒซ๋ฒˆ์งธ์ธ๋ฑ์Šค์ธ dispatch๊ฐ€ ์ƒํƒœ๋ณ€ํ™”๋ฅผ ์ผ์œผํ‚ค๋Š”๋ฐ(=raise) ์ผ์–ด๋‚œ ์ƒํƒœ๋ณ€ํ™”๋ฅผ reducer๊ฐ€ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค
๋‘๋ฒˆ์งธ ์ธ์ž๋Š” count state์˜ ์ดˆ๊ธฐ๊ฐ’์ด ๋œ๋‹ค

๐ŸŽฏ์ค‘๊ฐ„ ์ •๋ฆฌ
useReducer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ count๋ผ๋Š” state๋ฅผ ๋งŒ๋“ค๊ฒŒ๋˜๋ฉด ์ดˆ๊ธฐ๊ฐ’์ด 1๋กœ ํ• ๋‹น์ด๋˜๊ณ , count state๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ ์‹ถ์œผ๋ฉด ์ƒํƒœ๋ณ€ํ™”ํ•จ์ˆ˜์ธ dispatch๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒํƒœ๋ณ€ํ™”๋ฅผ ์ผ์œผํ‚ค๋ฉด ์ƒํƒœ๋ณ€ํ™” ์ฒ˜๋ฆฌํ•จ์ˆ˜์ธ reducer๊ฐ€ ์ฒ˜๋ฆฌํ•˜๊ฒŒ๋œ๋‹ค

dispatch({ type: 1 })
return์„ ๋ณด๋ฉด dispatch๊ฐ€ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค(๊ฐ์ฒด ์•ˆ์—๋Š” type์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ)์„ ์ „๋‹ฌํ•˜๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๋ฅผ Action๊ฐ์ฒด ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
(์ƒํƒœ๋ณ€ํ™”๋ฅผ ์„ค๋ช…ํ•  ๊ฐ์ฒด, Action์€ ์ƒํƒœ๋ณ€ํ™”๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค)

dispatch(์ƒํƒœ๋ณ€ํ™”ํ•จ์ˆ˜)๋ฅผ ํ†ตํ•˜์—ฌ ์ „๋‹ฌ๋œ action๊ฐ์ฒด๋Š” ์˜ค๋ฅธ์ชฝ ๋ถ„๋ฆฌ๋œ reducer๋กœ ์ „๋‹ฌ๋˜์–ด ์ƒํƒœ๋ณ€ํ™”์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” reducer๊ฐ€ ์ฒ˜๋ฆฌํ•˜๊ฒŒ๋œ๋‹ค.

const reducer = [state, action] => {}
ํ˜ธ์ถœ๋œ reducerํ•จ์ˆ˜๋Š”
์ฒซ๋ฒˆ์งธ์ธ์ž๋กœ๋Š” ํ˜„์žฌ์ƒํƒœ์ธ state๋ฅผ ๋ฐ›๊ณ ,
๋‘๋ฒˆ์งธ์ธ์ž๋กœ๋Š” dispatch๋ฅผ ํ˜ธ์ถœํ• ๋•Œ ์ „๋‹ฌํ•ด์ฃผ์—ˆ๋˜ Action๊ฐ์ฒด๋ฅผ ๋ฐ›๋Š”๋‹ค.


๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปReact ๊ณต์‹๋ฌธ์„œ

const [state, dispatch] = useReducer(reducer, initialArg, init);
  • useState์˜ ๋Œ€์ฒด ํ•จ์ˆ˜
  • (state, action) => newState์˜ ํ˜•ํƒœ๋กœ reducer๋ฅผ ๋ฐ›๊ณ  dispatch ๋ฉ”์„œ๋“œ์™€ ์ง์˜ ํ˜•ํƒœ๋กœ ํ˜„์žฌ state๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณต์žกํ•œ ์ •์  ๋กœ์ง์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋‚˜ ๋‹ค์Œ state๊ฐ€ ์ด์ „ state์— ์˜์กด์ ์ธ ๊ฒฝ์šฐ์— ๋ณดํ†ต useState๋ณด๋‹ค useReducer๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค.
  • useReducer๋Š” ์ž์„ธํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ์€ ์ฝœ๋ฐฑ ๋Œ€์‹  dispatch๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ฃผ์˜
React๋Š” dispatch ํ•จ์ˆ˜์˜ ๋™์ผ์„ฑ์ด ์•ˆ์ •์ ์ด๊ณ  ๋ฆฌ๋ Œ๋”๋ง ์‹œ์—๋„ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฆฌ๋ผ๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด useEffect๋‚˜ useCallback ์˜์กด์„ฑ ๋ชฉ๋ก์— ์ด ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์€ ์ด์œ ์ž…๋‹ˆ๋‹ค.


๐Ÿš€์ฐธ๊ณ ์ž๋ฃŒ

useReducer()
React๊ณต์‹๋ฌธ์„œ-useReducer()
React๊ฐ•์˜-์ด์ •ํ™˜๊ฐ•์‚ฌ

profile
FrontEnd Developer (with ๊ตฌ๊ธ€์‹ )

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