π https://github.com/happypoulp/redux-tutorial/wiki
Tutorial 05 - combine reducers
reducer κ° λμ§ μ΄ν΄νκΈ° μμνλ€β¦
var reducer_0 = function (state = {}, action) {
console.log('reducer_0 μ΄ μ λ¬λ°μ μν', state, 'μ μ‘μ
', action)
switch (action.type) {
case 'SAY_SOMETHING':
return {
...state,
message: action.value
}
default:
return state;
}
}
β¦ κ·Έλ°λ° λ λμκ°κΈ°μ μ, λ§μ½ μμ² λ§μ μ‘μ λ€μ΄ μλ€λ©΄ reducerλ€μ΄ μ΄λ»κ² λ°μ μ§ κ³ λ―Όν΄λ΄μΌνλ€:
var reducer_1 = function (state = {}, action) {
console.log('reducer_0 μ΄ μ λ¬λ°μ μν', state, 'μ μ‘μ
', action)
switch (action.type) {
case 'SAY_SOMETHING':
return {
...state,
message: action.value
}
case 'DO_SOMETHING':
// ...
case 'LEARN_SOMETHING':
// ...
case 'HEAR_SOMETHING':
// ...
case 'GO_SOMEWHERE':
// ...
// etc.
default:
return state;
}
}
μ΄λ κ²λ λ¨ νλμ reducer ν¨μκ° μ ν리μΌμ΄μ μ λͺ¨λ μ‘μ λ€μ λ€λ£° μ μλ€ (ν μλ μκ² μ§λ§, μ΄λλ‘ μ μ§ν μ μλ€β¦).
λ€νν Reduxλ reducer κ° νλμΈμ§ μ¬λ¬κ°μΈμ§ μ κ²½μ°μ§μκ³ , λ§μ λ κ·Έκ²λ€μ ν©μΉ μ μκ² λμμ€λ€.
reducer 2κ°λ₯Ό μ μΈν΄λ³΄μ.
var userReducer = function (state = {}, action) {
console.log('userReducer μ΄ μ λ¬λ°μ μν', state, 'μ μ‘μ
', action)
switch (action.type) {
// etc.
default:
return state;
}
}
var itemsReducer = function (state = [], action) {
console.log('itemsReducer μ΄ μ λ¬λ°μ μν', state, 'μ μ‘μ
', action)
switch (action.type) {
// etc.
default:
return state;
}
}
κ° λ¦¬λμμ μ£Όμ
λ μ΄κΈ° μνκ°μ μ 보면: userReducer
λ κ°μ²΄λ¦¬ν°λ΄({}
)μ ννμΈ λ°λ©΄ itemsReducer
λ μ΄κΈ° μνκ°μΌλ‘ λ°°μ΄([]
) μ λ°μλ€. μ΄κ²μ λ¨μν 리λμλ μ΄λ€ νμ
μ λ°μ΄ν° ꡬ쑰λ λ°μ μ μλ€λ κ²μ 보μ¬μ£ΌκΈ° μν¨μ΄λ€. μ΄λ€ λ°μ΄ν° ꡬ쑰λ₯Ό μ νν μ§λ κ·Έλ κ·Έλ λ€λ₯΄λ€ (κ°μ²΄, λ°°μ΄, boolean, string, immutable structre, β¦).
μ΄λ°μμΌλ‘ μ¬λ¬κ°μ redcuer μ κ·Ό λ°©μμΌλ‘ κ° reducerκ° μ ν리μΌμ΄μ μνμ μΌλΆλ§μ λ€λ£¨κ²λλ€.
νμ§λ§ μλ€μνΌ, createStore
λ μ€μ§ νλμ reducer ν¨μλ₯Ό λ°λλ€.
μ΄λ»κ² reducer λ€μ ν©μΉ μ μμκΉ? κ·Έλ¦¬κ³ μ΄λ»κ²νλ©΄ Reduxκ° κ° reducerλ€μ μνμ μΌλΆλ§ λ€λ£¨κ² ν μ μμκΉ?
κ°λ¨νλ€. Reduxμ combineReducers
ν¨μλ₯Ό μ¬μ©νλ€. combineReducers
λ ν΄μλ₯Ό κ°μ§κ³ ν¨μλ₯Ό 리ν΄νκ³ , μ€νν λ λͺ¨λ reducerλ€μ νΈμΆνκ³ , μλ‘μ΄ μν μ‘°κ°λ€μ Reduxκ° κ°κ³ μλ νλμ μν κ°μ²΄(λ¨μν ν΄μ {})λ‘ μ‘°ν©νλ€.
μκΈ°κ° κΈΈμλλ° μ΄λ°μμΌλ‘ reducer κ° μ¬λ¬κ°μΌ λ Redux μΈμ€ν΄μ€λ₯Ό λ§λ λ€:
import { createStore, combineReducers } from 'redux'
var reducer = combineReducers({
user: userReducer,
items: itemsReducer
})
// Output:
// userReducer μ΄ μ λ¬λ°μ μν {} μ μ‘μ
{ type: '@@redux/INIT' }
// userReducer μ΄ μ λ¬λ°μ μν {} μ μ‘μ
{ type: '@@redux/PROBE_UNKNOWN_ACTION_9.r.k.r.i.c.n.m.i' }
// itemsReducer μ΄ μ λ¬λ°μ μν [] μ μ‘μ
{ type: '@@redux/INIT' }
// itemsReducer μ΄ μ λ¬λ°μ μν [] μ μ‘μ
{ type: '@@redux/PROBE_UNKNOWN_ACTION_4.f.i.z.l.3.7.s.y.v.i' }
var store_0 = createStore(reducer)
// Output:
// userReducer μ΄ μ λ¬λ°μ μν {} μ μ‘μ
{ type: '@@redux/INIT' }
// itemsReducer μ΄ μ λ¬λ°μ μν [] μ μ‘μ
{ type: '@@redux/INIT' }
outputμ 보면 μκ² μ§λ§, κ° reducerλ μ΄κΈ° μ‘μ
@@redux/INIT
μ μ ννκ² νΈμΆνλ€.
κ·Έλ°λ° λ€λ₯Έ μ‘μ
μ λμ§? combineReducers
μμ μ€νλ sanity check μ΄λ€. μ΄κ²μ reducerκ° μνλ₯Ό 리ν΄ν λ undefined
κ° μλλΌλ κ²μ νμΈνλ€.
combineReducers
μμ init actionμ 첫λ²μ§Έ νΈμΆ λν κ°μ λͺ©μ μ 곡μ νλ€. (sanity check λ₯Ό νλ κ²)
console.log('μ΄κΈ°ν μ΄ν store_0 μ μν:', store_0.getState())
// Output:
// μ΄κΈ°ν μ΄ν store_0 μ μν: { user: {}, items: [] }
Reduxκ° κ° μνλ€μ μλ²½νκ² μ²λ¦¬νκ³ μλ€.
λ§μ§λ§ μνλ μ€μ λ‘ userReducer
μ itemsReduces
λ‘ λ§λ€μ΄μ§ λ¨μν ν΄μλ€.
{
user: {}, // {} userReducer μμ 리ν΄λ μ‘°κ°
items: [] // [] itemsReducer μμ 리ν΄λ μ‘°κ°
}
reducerλ€μ μνλ₯Ό νΉμ κ°(userReducer
λ {}
, itemsReducer
λ []
)μΌλ‘ μ΄κΈ°ννκΈ°λλ¬Έμ λ§μ§λ§ μνμμ μ΄ κ°μ΄ λμ¨ κ²μ λΉμ°νλ€.
μ΄μ reducer κ° μ΄λ»κ² λμνλμ§ μκ²λμμΌλκΉ μ‘μ μ dispatchνμ λ Redux μνμ μν₯μ μ£Όλ κ²μ λ΄λ³΄μ.
λ€μ: 06_dispatch-action.md