Frontend/React

[Redux] ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ(SoC)

rachel_13 2022. 8. 14. 04:37

๐Ÿ‘‰ ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ (Seperation of Concerns)

: ํŠน์ •ํ•œ ๊ด€์‹ฌ์‚ฌ์— ๋”ฐ๋ผ ๊ธฐ๋Šฅ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ, ๊ฐ ๊ธฐ๋Šฅ๋“ค๋งˆ๋‹ค ๊ฐœ๊ฐœ์˜ ๊ด€์‹ฌ์‚ฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

 ํ•จ์ˆ˜๋ฅผ ์ตœ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•˜์—ฌ, ํ”„๋กœ์„ธ์Šค์˜ ๋ณต์žก๋„๋ฅผ ๋‚ฎ์ถ”๊ณ , ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๋ฉฐ, ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์›ํ• ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฆฌ๋•์Šค๋„ ์ด "๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ"๋ฅผ ํ†ตํ•ด ํด๋” ๊ตฌ์กฐ๋ฅผ ๋‚˜๋ˆ„์–ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ,

ํฌ๊ฒŒ ๋‚˜๋‰  ์ˆ˜ ์žˆ๋Š” ํŒŒํŠธ๊ฐ€ "๋ฆฌ๋“€์„œ(reducer)"์™€ "์•ก์…˜(action)" ์ด๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์œ ์ €์™€ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ๋“ค์€ user.js ์— ๋‹ค ๋„ฃ์–ด๋‘๊ณ  ์‚ฌ์šฉ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฒŒ์‹œ๊ธ€๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ๋“ค์€ boards.js์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

๋ฆฌ๋“€์„œ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š”, ์ˆœ์ˆ˜ํ•จ์ˆ˜๋กœ ์ •์˜๊ฐ€ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ฆฌํ•˜๊ธฐ ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•˜๋‹ค.

const init = {
	id: 1,
    user_name: 'Tom',
    is_logged_in: false,
    is_admin: false,
}

export const userReducer = (prevState, action)=> {
	switch(action.type){
    	case 'IS_LOGGED':
        return ({
        	...prevState,
            is_logged_in: true,
        })
        default: return prevState;
}

โœ… ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ๋œ ์ž‘์€ ๋ฆฌ๋“€์„œ๋“ค ํ•ฉ์น˜๊ธฐ : combineReducers

const {combineReducers} = require('redux')
const userReducer = require('./user');
const boardReducer = require('./board');

module.exports = combineReducers({
    user: userReducer,
    boards: boardReducer
})