์๋ก์ด CombineReducers ๋ฌธ์๋ ๊ด์ฐฎ์ง๋ง, ๋ฆฌ๋์๋ฅผ ๊ด๋ฆฌํ๋ ์ํ์ ๋น์ฌ์๋ก ์ด๋ฆ์ ์ง์ ํด์ผ ํ๋ค๋ ์ค๋ช ์ ์์ต๋๋ค.
๊ฑด๋ ๋ฐ์ง ์๊ณ ์์ ํ ์ฝ์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋งํฉ๋๋ค.
๊ฒฐ๊ณผ ๋ฆฌ๋์๋ ๋ชจ๋ ์์ ๋ฆฌ๋์๋ฅผ ํธ์ถํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋จ์ผ ์ํ ๊ฐ์ฒด๋ก ์์งํฉ๋๋ค. ์ํ ๊ฐ์ฒด์ ๋ชจ์์ ์ ๋ฌ๋ ๋ฆฌ๋์์ ํค์ ์ผ์นํฉ๋๋ค.
๊ทธ๋ฌ๋ ์ฌ๋๋ค์ด ์ข
์ข
์ด ๋ฌธ์ฅ์ ๋์น๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ด ๋ ๋๋๋ฌ์ ธ์ผ ํ๋ค๋ ๋ฐ ๋์ํฉ๋๋ค.
#399์ ๋ณ๊ฒฝ ์ฌํญ์ด ๋์์ด ๋ ๊น์?
๋๋ ์ปดํจํฐ ๊ณผํ์ ๋ ์ ํธํ๊ณ , ์๋ฅผ ๋ ์งํ์ ์ด ๊ท์น ์ ๋ํด ๋ช ์์ ์ผ๋ก ์ธ๊ธํฉ๋๋ค.
๋ฆฌ๋์์ ์ด๋ฆ์
todos
๋ฐcounter
-- ์ ํํ ์ฐ๋ฆฌ๊ฐ ์ ๋ฌํ๋ ์ํ์ ์ผ๋ถ์ ๋๋ค.์ํ์ ์ผ๋ถ๋ฅผ ๋ฆฌ๋์ค์ ์ ๋ฌํ๊ธฐ ์ํด Redux๋ _convention_์ ์ฌ์ฉํฉ๋๋ค. ๋ฆฌ๋์๋ ์ ๋ฌํ๋ ค๋ ์ํ์ ์ผ๋ถ๋ก ์ ํํ ์ด๋ฆ์ ์ง์ ํด์ผ ํฉ๋๋ค.
#399๊ฐ ์ข์ ๊ฒ ๊ฐ์ง๋ง _Flux ์ฌ์ฉ์_๋ฅผ ์ํ ์ค๋ช ์ ๋๋ค. ๋๋ ๋ณดํธ์ ์ ์ ํ Redux์ ์ค๊ณ ์์ต๋๋ค. ๋ด ํ ์คํธ์ ์ผ๋ถ ํํ๋ ์บ์ฃผ์ผ JavaScript ๊ฐ๋ฐ์์๊ฒ ๋ ๋์์ด ๋ ๊ฒ์ ๋๋ค.
์ค์ํ ์์ด๋์ด๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ๊ท์น์ API์ ์ผ๋ถ์
๋๋ค . ์ด๋ createRedux
, createDispatcher
๋ฐ ๊ธฐํ API ํจ์์๋ ๋๊ฐ์ด ์ค์ํฉ๋๋ค. API์ ์ผ๋ถ์ด๊ธฐ ๋๋ฌธ์ ํญ์ ๋ช
์์ ์ผ๋ก ๊ท์ฝ์ ๋ช
์ํ์ญ์์ค.
Btw, ๋๋ ์ฌ์ ํ ์ํ์ ํ์ ๋ถ๋ถ์ ๊ฐ์๊ธฐ์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์ํดํฉ๋๋ค. ๋ด๊ฐ camelCase ๊ทธ๋ค ๋๋ ๋ญ๊ฐ ๊ฐ์
ํด์ผํฉ๋๊น? state={owner: {name: 'John'} }
โ export function ownerName(state = [], action)
?
ํผ๋๋ฐฑ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๋งค์ฐ ๊ฐ์น๊ฐ ์์ต๋๋ค.
๋๋ ๊ทธ๊ฒ์ ๊ฐ์กฐํ๊ณ ์ถ๋ค
์ปจ๋ฒค์ ์ API์ ์ผ๋ถ์ ๋๋ค
์ฌ์ค์ด ์๋๋๋ค.
๋ฌธ์์์ import *
๋ ํผํด์ผ ํฉ๋๋ค. ์๋ํ๋ฉด ์ฌ๋๋ค์ ์ด๊ฒ์ด API์ ํ์์ ์ธ ๋ถ๋ถ์ด ์๋ ๋ ํ์ ์์๋ผ๊ณ ๊ฐ์ ํ๊ณ ์ ๊ฐ ์ฌ์ฉํ๋ ํธ๋ฆฌํ ๋ฐ๋ก ๊ฐ๊ธฐ์ผ ๋ฟ์
๋๋ค.
ํจ์ ์ด๋ฆ์ ES6 export
๋ฐ import * as
์๋ ๋ฐฉ์ ๋๋ฌธ์ ์ค์ํฉ๋๋ค.
Btw, ๋๋ ์ฌ์ ํ ์ํ์ ํ์ ๋ถ๋ถ์ ๊ฐ์๊ธฐ์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์ํดํฉ๋๋ค. ๋ด๊ฐ camelCase ๊ทธ๋ค ๋๋ ๋ญ๊ฐ ๊ฐ์ ํด์ผํฉ๋๊น? state={์์ ์: {์ด๋ฆ: 'John'} } โ export function ownerName(state = [], action)?
์๋ :-). ๊ทธ๊ฒ์ ๋ง๋ฒ์ API๊ฐ ์๋๋๋ค. combineReducers(object)
๋ ์ฌ๋ฌ ๊ฐ์๊ธฐ๋ฅผ ํ๋๋ก ๊ฒฐํฉํ๊ณ ์ ๊ณตํ ํค๋ก ์ํ์ ์ผ๋ถ๋ฅผ ๊ฐ์ผ๋ก ์ ๋ฌํฉ๋๋ค. ์ ํํ ํ ์์ค ๊น์ด์์๋ง ์ด ์์
์ ์ํํฉ๋๋ค. "๋๋ฌด ์ ์ฒด๋ฅผ ๋ง๋๋ ๋ง๋ฒ"์ ์์ต๋๋ค. ๊ฐ์๊ธฐ๋ฅผ ๋ ๋ง์ ๊ธฐ๋ฅ์ผ๋ก ๋๋๋ ๊ฒ์ ๋น์ ์๊ฒ ๋ฌ๋ ค ์์ต๋๋ค.
// Function names don't matter!
function processA(state, action) { ... }
function doSomethingWithB(state, action) { ... }
function reducer(state = {}, action) {
return {
// Because you call them!
a: processA(state.a, action),
b: doSomethingWithB(state.b, action)
};
}
// Not using combineReducers
let store = createStore(reducer);
์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํ combineReducers
๋์ฐ๋ฏธ ๋ฅผ ์ฌ์ฉํด ๋ ์๊ด ์์ต๋๋ค.
// Function names don't matter!
function processA(state, action) { ... }
function doSomethingWithB(state, action) { ... }
/*
function reducer(state = {}, action) {
return {
a: processA(state.a, action),
b: doSomethingWithB(state.b, action)
};
}
*/
let reducer = combineReducers({
a: processA,
b: doSomethingWithB
});
let store = createStore(reducer);
์ด ์์ ์ ์ฌ๋ฌ ๋ฒ ์ํํ ์ ์์ต๋๋ค.
์ ์:
// Function names don't matter!
function processSomePartOfA(state, action) { ... }
function doSomethingWithOtherPartOfA(state, action) { ... }
function processA(state, action) {
return {
// Because you call them!
somePart: processSomePartOfA(state.somePart),
otherPart: doSomethingWithOtherPartOfA(state.otherPart)
}
}
function doSomethingWithB(state, action) { ... }
function reducer(state = {}, action) {
return {
// Because you call them!
a: processA(state.a, action),
b: doSomethingWithB(state.b, action)
};
}
// Not using the helper
let store = createStore(reducer);
์๊ฒ ์ด? ํจ์๋ฅผ ํธ์ถํ๋ ํจ์์ผ ๋ฟ์
๋๋ค. ๋ง๋ฒ์ "๊น์ ๋ฌผ๊ฑด"์ด ์์ต๋๋ค.
combineReducers
์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
// Function names don't matter!
function processSomePartOfA(state, action) { ... }
function doSomethingWithOtherPartOfA(state, action) { ... }
/*
function processA(state, action) {
return {
somePart: processSomePartOfA(state.somePart),
otherPart: doSomethingWithOtherPartOfA(state.otherPart)
}
}
*/
let processA = combineReducers({
somePart: processSomePartOfA,
otherPart: doSomethingWithOtherPartOfA
});
function processA(state, action) { ... }
function doSomethingWithB(state, action) { ... }
/*
function reducer(state = {}, action) {
return {
a: processA(state.a, action),
b: doSomethingWithB(state.b, action)
};
}
*/
let reducer = combineReducers({
a: processA,
b: doSomethingWithB
});
let store = createStore(reducer);
๋ฌธ์ ํจ์ ์ด๋ฆ์ ์ฌ์ฉํ ๋์
๋๋ค ์ ์ผํ ๋ถ๋ถ export
+ import * as
์ ๋น์ ์ด์ ์ ๋ฌ ๊ฐ์ฒด "์ทจ๋" combineReducers
_that์ด๊ธฐ ๋๋ฌธ์ ์ผ๋ง๋ import *
works_ ! ๋ด๋ณด๋ด๊ธฐ ํค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฌผ๊ฑด์ ๊ฐ์ฒด์ ๋ฃ์ต๋๋ค.
๋ด ๊ฐ์ฅ ํฐ ์ค์๋ ๋ ์๊ฐ ๋ช ๋ช ๋ ๋ด๋ณด๋ด๊ธฐ์ ์ต์ํ๋ค๊ณ ๊ฐ์ ํ๋ ๊ฒ์ ๋๋ค.
๋ด ๊ฐ์ฅ ํฐ ์ค์๋ ๋ ์๊ฐ ๋ช ๋ช ๋ ๋ด๋ณด๋ด๊ธฐ์ ์ต์ํ๋ค๊ณ ๊ฐ์ ํ๋ ๊ฒ์ ๋๋ค.
๋ ์๊ฐ ES6์ ์ต์ํ๋ค๊ณ ๊ฐ์ ํ๋ ๊ฒ์ ๋ฌด๋ฆฌ๋ผ๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ด ์ฌ๋๋ค์ด ๋ฐฐ์ฐ๊ณ ์ด๊ฒ์ ์์๋ด๋๋ก ๋ง๋๋ ๊ฒ์ ๋๋ค. ๋ฐ๋ผ์ ์ฝ๊ธฐ๊ฐ ๋ ์์ ์ง์๊ณผ ๊ฒฝํ๋ณด๋ค ์์ ์๋ค๋ ๊ฒ์ ์ค์ ๋ก ์ข์ ์ผ์ ๋๋ค.
์ฐ๋ฆฌ๋ ๋ช
์ ์ ์ผ๋ก ํธ์ถํ๋ ์์ ๋ฅผ ๋ณ๊ฒฝํ๊ณ combineReducers
์์ reducers/index
๊ฐ ์ง๊ธ๋ถํฐ ๋ ์๋ฏธ๋ฅผ ๋ง๋ค๋ ค๊ณ , ๊ทธ๋์ ํฌ๋ง : https://github.com/gaearon/redux/pull/473
์ด๊ฒ์ ํ์ฌ ๋ฌธ์์์ ๋ ์ ๋ค๋ฃจ์ด์ง๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ธฐ ๋๋ฌธ์ ๋ซ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฌธ์์์๋ ๋ ์ด์ import *
๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๋ค.
๊ทธ๋ฐ ์์์ ๋์์ ํฌํจํ๋ ๊ฒ์ด ์ข์ ์ฝ๋ฉ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ์ ๋ง ์๊ฐํ์ญ๋๊น?
๋์๊ฒ ๊ทธ๊ฒ์ ์ฝ๋๋ฅผ ๋๋ ํํ๊ณ CombineReducer๊ฐ ์กด์ฌํด์๋ ์๋๋ฉฐ, ๋ถํ์ํ ์ถ์ํ ๋จ๊ณ๋ฅผ ์ถ๊ฐํ๊ณ ํ๋ก์ธ์ค๋ฅผ ๋ณต์กํ๊ฒ ๋ง๋ญ๋๋ค.
ํ๋ ์์ํฌ๋ฅผ ์์ฑํ ๋ ์ด์ ๊ฐ์ ์ค์ํ ๊ธฐ๋ฅ์ ์ฝ๊ฒ ์ดํดํ ์ ์์ด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด "๋ง๋ฒ ๊ฐ์" ๋๋์ ๊ทธ๋ ์ง ์์ต๋๋ค.
์ถ์ : ๋๋ ๊ณต์ Redux ๋ฌธ์์์ ์์ต๋๋ค : "๋ง๋ฒ์ด ์๋๋๋ค"
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
ํผ๋๋ฐฑ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๋งค์ฐ ๊ฐ์น๊ฐ ์์ต๋๋ค.
๋๋ ๊ทธ๊ฒ์ ๊ฐ์กฐํ๊ณ ์ถ๋ค
์ฌ์ค์ด ์๋๋๋ค.
๋ฌธ์์์
import *
๋ ํผํด์ผ ํฉ๋๋ค. ์๋ํ๋ฉด ์ฌ๋๋ค์ ์ด๊ฒ์ด API์ ํ์์ ์ธ ๋ถ๋ถ์ด ์๋ ๋ ํ์ ์์๋ผ๊ณ ๊ฐ์ ํ๊ณ ์ ๊ฐ ์ฌ์ฉํ๋ ํธ๋ฆฌํ ๋ฐ๋ก ๊ฐ๊ธฐ์ผ ๋ฟ์ ๋๋ค.ํจ์ ์ด๋ฆ์ ES6
export
๋ฐimport * as
์๋ ๋ฐฉ์ ๋๋ฌธ์ ์ค์ํฉ๋๋ค.์๋ :-). ๊ทธ๊ฒ์ ๋ง๋ฒ์ API๊ฐ ์๋๋๋ค.
combineReducers(object)
๋ ์ฌ๋ฌ ๊ฐ์๊ธฐ๋ฅผ ํ๋๋ก ๊ฒฐํฉํ๊ณ ์ ๊ณตํ ํค๋ก ์ํ์ ์ผ๋ถ๋ฅผ ๊ฐ์ผ๋ก ์ ๋ฌํฉ๋๋ค. ์ ํํ ํ ์์ค ๊น์ด์์๋ง ์ด ์์ ์ ์ํํฉ๋๋ค. "๋๋ฌด ์ ์ฒด๋ฅผ ๋ง๋๋ ๋ง๋ฒ"์ ์์ต๋๋ค. ๊ฐ์๊ธฐ๋ฅผ ๋ ๋ง์ ๊ธฐ๋ฅ์ผ๋ก ๋๋๋ ๊ฒ์ ๋น์ ์๊ฒ ๋ฌ๋ ค ์์ต๋๋ค.์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํ
combineReducers
๋์ฐ๋ฏธ ๋ฅผ ์ฌ์ฉํด ๋ ์๊ด ์์ต๋๋ค.์ด ์์ ์ ์ฌ๋ฌ ๋ฒ ์ํํ ์ ์์ต๋๋ค.
์ ์:
์๊ฒ ์ด? ํจ์๋ฅผ ํธ์ถํ๋ ํจ์์ผ ๋ฟ์ ๋๋ค. ๋ง๋ฒ์ "๊น์ ๋ฌผ๊ฑด"์ด ์์ต๋๋ค.
combineReducers
์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.๋ฌธ์ ํจ์ ์ด๋ฆ์ ์ฌ์ฉํ ๋์ ๋๋ค ์ ์ผํ ๋ถ๋ถ
export
+import * as
์ ๋น์ ์ด์ ์ ๋ฌ ๊ฐ์ฒด "์ทจ๋"combineReducers
_that์ด๊ธฐ ๋๋ฌธ์ ์ผ๋ง๋import *
works_ ! ๋ด๋ณด๋ด๊ธฐ ํค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฌผ๊ฑด์ ๊ฐ์ฒด์ ๋ฃ์ต๋๋ค.๋ด ๊ฐ์ฅ ํฐ ์ค์๋ ๋ ์๊ฐ ๋ช ๋ช ๋ ๋ด๋ณด๋ด๊ธฐ์ ์ต์ํ๋ค๊ณ ๊ฐ์ ํ๋ ๊ฒ์ ๋๋ค.