๐Ÿ‘พRedux์˜ ํ•„์š”์„ฑ

๊ถŒ๊ทœ๋ฆฌยท2023๋…„ 6์›” 5์ผ
0

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปFrontEnd

๋ชฉ๋ก ๋ณด๊ธฐ
23/29
post-thumbnail

๐Ÿ“ข ์—ฌ๋Š” ๋ง

๋ฆฌ๋•์Šค์— ๋Œ€ํ•˜์—ฌ ๊ณต๋ถ€ํ•˜๋˜ ์ค‘, ๋ฆฌ๋•์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์ฝ”๋“œ์™€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ์˜ ์ฝ”๋“œ ์ฐจ์ด๊ฐ€ ๊ถ๊ธˆํ•˜์—ฌ ์‹ค์Šต์„ ํ•ด๋ณด์•˜๋‹ค. ์‹ค์Šต์˜ ๋‚ด์šฉ์€ red ๋ฒ„ํŠผ , green ๋ฒ„ํŠผ , blue ๋ฒ„ํŠผ์„ ๋งŒ๋“ค๊ณ  ๊ฐ๊ฐ์˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ชจ๋“  ๋ฒ„ํŠผ์˜ ์˜์—ญ์ด ๋ˆ„๋ฅธ ๋ฒ„ํŠผ์˜ ์ƒ‰์œผ๋กœ ๋ณ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


01. Redux๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ

์ด๋ ‡๊ฒŒ RED, BLUE, GREEN ์„ธ ๊ฐœ์˜ ๋ฒ„ํŠผ์ด ์žˆ๋‹ค. ์šฐ์„  ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ทธ ๋ฒ„ํŠผ์˜ ์ƒ‰์— ๋งž๊ฒŒ ๋ณ€ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ช‡ ๊ฐœ์˜ ๋กœ์ง์ด ํ•„์š”๋กœ ํ•˜๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณด์•˜๋‹ค.

๐Ÿ–ฑ๏ธโค๏ธ ์˜ˆ๋ฅผ ๋“ค์–ด, RED ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋นจ๊ฐ„ ๋ฒ„ํŠผ์ด ํ•ด์•ผํ•  ์ผ

1. ์ž๊ธฐ ์ž์‹ ์˜ ์ƒ‰์„ ์œ ์ง€ํ•ด์•ผ ํ•œ๋‹ค.
2. ์ดˆ๋ก ๋ฒ„ํŠผ๋„ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ฐ”๋€Œ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค.
3. ํŒŒ๋ž€ ๋ฒ„ํŠผ๋„ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ฐ”๋€Œ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค.

RED ๋ฒ„ํŠผ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ BLUE, GREEN ๋ฒ„ํŠผ๋„ ํ•ด์•ผํ•  ์ผ์ด 3๊ฐ€์ง€๋‹ˆ๊นŒ ๋ฒ„ํŠผ 3๊ฐœ์˜ ์ด ๋กœ์ง์€ 9๊ฐœ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋‹ค.


1. ์ž‘์„ฑํ•œ ์ฝ”๋“œ

    <div id="red"></div>
    <div id="green"></div>
    <div id="blue"></div>

์šฐ์„  ๋ฒ„ํŠผ๋ณ„๋กœ id ๊ฐ’์„ ์ฃผ์—ˆ๋‹ค. ์ด์ œ ๊ฐ๊ฐ์˜ ์ƒ‰๊น”์˜ ์ด๋ฆ„์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด id ๊ฐ’์„ document.querySelector์„ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ด๋‹ค.

function red(){
    document.querySelector('#red').innerHTML=
        `
        <div class="container" id="conponent_red">
        <h1>red</h1>
        <input type="button" value="fire">
        </div>
        `;
}
red();

์œ„์˜ ์ฝ”๋“œ๋Š” RED ๋ฒ„ํŠผ์˜ ๋ถ€๋ถ„์ด๋‹ค. ๋ฒ„ํŠผ์— onclick ์ด๋ฒคํŠธ ๋ฅผ ์ค˜์„œ, ๋ฒ„ํŠผ์ด ํด๋ฆญ ๋˜์—ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๋ฒ„ํŠผ('#component_')์˜ ์ƒ‰๊น”๋„ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

2. ๋Š๋‚€ ์ 

๐Ÿค” ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋‹ˆ, ๋งŒ์•ฝ ์ƒ‰๊น”์ด 50 ๊ฐ€์ง€๋ผ๋ฉด ํ•จ์ˆ˜๋ฅผ 50๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ ์•„๋“ํ–ˆ๋‹ค. ๋˜ํ•œ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋„ 3๊ฐ€์ง€๊ฐ€ ์•„๋‹ˆ๋ผ N๊ฐœ๋กœ ๋Š˜์–ด๋‚ ํ…๋ฐ ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฑด ์‹ค์šฉ์ ์ด์ง€ ์•Š๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.


02. Redux๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

Redux๋Š” state ๊ฐ์ฒด๊ฐ€ ์ค‘์•™ ์ง‘์ค‘์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์—์„œ ๊ฒ€์ •์ƒ‰ ์›์„ state ๊ฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ์ด๋ฒˆ์—๋„ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ชจ๋‘ ๊ฐ™์€ ์ƒ‰์œผ๋กœ ๋ณ€ํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐœ์˜ ๋กœ์ง์ด ํ•„์š”ํ• ๊นŒ?

๐Ÿ–ฑ๏ธ ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฒ„ํŠผ์„ ๋ฒ„ํŠผ์ด ํ•ด์•ผํ•  ์ผ

1. ์ž๊ธฐ ์ž์‹ ์˜ ์ƒ‰์„ ์œ ์ง€ํ•ด์•ผ ํ•œ๋‹ค.
2. ์ž๊ธฐ ์ž์‹ ์˜ ์ƒ‰์„ state์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค. 
   = ๊ทธ๋Ÿผ state๊ฐ€ ๋‹ค๋ฅธ ๋ฒ„ํŠผ์—๊ฒŒ ์•Œ๋ ค์ค„ ๊ฒƒ.

์ด๋ฒˆ์—๋Š” ํ•œ ๋ฒ„ํŠผ๋งˆ๋‹ค 2๊ฐœ์˜ ๋กœ์ง์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด 6๊ฐœ์˜ ๋กœ์ง์ด ์กด์žฌํ•œ๋‹ค. ํ™•์‹คํžˆ Redux๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ๊นŒ ์ „์ฒด ๋กœ์ง์ด ์ค„์—ˆ๋‹ค. ์ด ๋ง์€ ์ฝ”๋“œ๋„ ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.


1. ์ž‘์„ฑํ•œ ์ฝ”๋“œ

let store= Redux.createStore(reducer);

Redux๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ store์„ ์ƒ์„ฑํ–ˆ๋‹ค. ์ด ๋•Œ ์ธ์ž๋กœ reducer ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.

function reducer(state,action){
   	if(state===undefined){
    	return {color:"yellow"}
    }
}  

๐Ÿ“ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด if๋ฌธ์ด ์žˆ๋‹ค.
reducer ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ๋•Œ state์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ฒ˜์Œ์—๋Š” ๊ฐ’์ด undefined์ด๋ฏ€๋กœ ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์–ด ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ตฌ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ดˆ๊ธฐ๊ฐ’์— color : "yellow"์ด๋ฏ€๋กœ ์ฝ˜์†”์„ ํ™•์ธํ•˜๋ฉด ๋ฐ‘๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚œ๋‹ค.
state์˜ ์ดˆ๊ธฐ๊ฐ’๋„ ์„ค์ •์„ ํ–ˆ์œผ๋‹ˆ, RED ํ•จ์ˆ˜์— state ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ด๋‹ค.

let state = store.getState();

๐Ÿค” ํ•˜์ง€๋งŒ ๋นจ๊ฐ„์ƒ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋นจ๊ฐ„์ƒ‰์ด ๋‚˜์™€์•ผ ํ•˜๋Š”๋ฐ ์ดˆ๊ธฐ๊ฐ’์ด ๋…ธ๋ž€์ƒ‰์ด๋ผ ๋…ธ๋ž€์ƒ‰์ด ๋– ์„œ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค.

  1. reducer ํ•จ์ˆ˜์— if๋ฌธ์„ ์ถ”๊ฐ€.
let newState;
if(action.type ==='CHANGE_COLOR'){
   newState = Object.assign({}, state, {color:"red"})
}return newState;
  1. RED ํ•จ์ˆ˜์— innerHTML ์ˆ˜์ •
document.querySelector('#red').innerHTML= `
<div class="container" id="conponent_red" style="background-color:${state.color}">
	<h1>red</h1>
    <input type="button" value="fire">       
</div>
`
  1. ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ๋ฐ”๋€Œ๊ฑฐ๋‚˜ ์ˆ˜์ • ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉด subscribe ๊ตฌ๋ฌธ ์ถ”๊ฐ€
store.subscribe(red);

์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ BLUE์™€ GREEN๋„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์™„์„ฑ!

profile
๊ธฐ๋ก์žฅ ๐Ÿ“

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