Redux: DucksํŒจํ„ด์ด๋ž€?๐Ÿฆ†

๋‘์„ ์•„ Dusunaยท2022๋…„ 7์›” 29์ผ
0

๐Ÿ“– reference link:
1. Redux docs
https://redux.js.org/style-guide/#structure-files-as-feature-folders-with-single-file-logic


๋‹จ์ผ ํŒŒ์ผ ๊ตฌ์กฐ

Because of this,ย we recommend that most applications should structure files using a "feature folder" approachย (all files for a feature in the same folder). Within a given feature folder,ย the Redux logic for that feature should be written as a single "slice" file, preferably using the Redux Toolkitย createSliceAPI. (This is also known as theย "ducks" pattern). While older Redux codebases often used a "folder-by-type" approach with separate folders for "actions" and "reducers", keeping related logic together makes it easier to find and update that code.


๋•์Šค ํŒจํ„ด Ducks Pattern / ๋ชจ๋“ˆ module

  • Erik Rasmussen ์ œ์•ˆ

  • ์•ก์…˜ ํƒ€์ž…, ์•ก์…˜ ์ƒ์„ฑ ํ•จ์ˆ˜, ๋ฆฌ๋“€์„œ๋ฅผ ๊ฐ๊ฐ์˜ ๋ณ„๋„์˜ ํŒŒ์ผ(ํด๋”)๊ฐ€ ์•„๋‹ˆ๋ผ
    ํ•˜๋‚˜์˜ module์ฒ˜๋Ÿผ ํ•œ ํŒŒ์ผ ์•ˆ์— ์ž‘์„ฑํ•œ๋‹ค.

    • src/modules/๋ชจ๋“ˆ๋ช….js
    • modules/index.js

์‚ฌ์šฉ์˜ˆ์‹œ

reducer๋Š” export default๋กœ ๋‚ด๋ณด๋ƒ„

export default function reducer( state={}, action={} ){
    switch (์•ก์…˜ํƒ€์ž…) {
    	// ๋ฆฌ๋“€์„œ 
    	default: return state;
    }
}

actionํ•จ์ˆ˜๋Š” export๋กœ ๋‚ด๋ณด๋ƒ„

export function createWidget( widget ){
    return { type: CREATE, widget }
}

์•ก์…˜ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ reducer/ACTION_TYPE ํ˜•ํƒœ๋กœ ์ ์Œ

const CREATE = "widgets/CREATE"

์ ‘๋‘์‚ฌ(reducer)๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š” ์ด์œ ?

  • ์„œ๋กœ ๋‹ค๋ฅธ ๋ฆฌ๋“€์„œ์—์„œ ์•ก์…˜์ด๋ฆ„์ด ์ค‘์ฒฉ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€

1. ํŒŒ์ผ ์œ ํ˜•์— ๋”ฐ๋ฅธ ํด๋” ๊ตฌ์กฐ

  • ๋‹จ์  : ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์„ ์ˆ˜์ •ํ•  ๋•Œ, ๊ธฐ๋Šฅ์— ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์•ผ ํ•จ

2. ๊ธฐ๋Šฅ ์ค‘์‹ฌ์˜ ํŒŒ์ผ ๊ตฌ์กฐ

  • ์žฅ์  : ๋‹จ์ผ ๊ธฐ๋Šฅ์„ ์ž‘์„ฑํ•  ๋•Œ, ํ•˜๋‚˜์˜ ํŒŒ์ผ๋งŒ ๋‹ค๋ฃจ๋ฏ€๋กœ ์ง๊ด€์ ์ธ ์ฝ”๋“œ ์ž‘์„ฑ
profile
์•ˆ๋…•ํ•˜์„ธ์š”.

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