μ΄κ²μ μ ν¨ν redux κ°μκΈ°μ λκΉ?
function reducer(state={ byId: {}, ids: [] }, action) {
switch (action.type) {
case 'ADD':
state = { ...state }
state.byId[action.id] = action.value
state.ids.push(action.id)
return state
default:
return state
}
}
μ 체 μνκ° λ¨Όμ μμ 볡μ λλ―λ‘ μ κ°μ²΄κ° λ°νλμ§λ§ λ΄λΆ κ°μ²΄λ κ·Έλλ‘ μ μ§λκ³ λ³κ²½λ©λλ€.
μλλ©΄ λ€μκ³Ό κ°μ΄ ν΄μΌ ν©λκΉ?
function reducer(state={ byId: {}, ids: [] }, action) {
switch (action.type) {
case 'ADD':
return {
...state,
byId: {
...state.byId,
[action.id]: action.value
},
ids: [ ...state.ids, action.id ]
}
default:
return state
}
}
첫 λ²μ§Έ μ κ·Ό λ°©μμμ λ³Ό μ μλ λΆμμ©μ DevTools LogMonitorμ μμ΅λλ€.
byId
)λ₯Ό νμ₯νλ©΄ μ΄μ μ λ€λ₯Έ νλͺ©μ΄μλλΌλ μ΅μ κ°κ³Ό λμΌν κ°μ νμν©λλ€.LogMonitorμ λ²κ·Έμ
λκΉ μλλ©΄ λ λ²μ§Έ μ κ·Ό λ°©μμ΄ μ¬λ°λ₯Έ κ²μ
λκΉ? λμ°λ³μ΄ μ μ state = _.cloneDeep(state)
λ₯Ό μΆκ°νλ©΄ μ€μ λ‘ μ λλ‘ μλνμ¬ LogMonitorκ° λμΌν κ°μ²΄λ₯Ό μ¬μ¬μ©νλ―λ‘ λμΌν κ°μ μ¦λͺ
ν©λλ€.
λλ¨Έμ§ μ±μ μ λλ‘ μλνκ³ @connect
보기λ μ¬λ°λ₯΄κ² μ
λ°μ΄νΈλ©λλ€.
μλμ, 첫 λ²μ§Έ μλ μνλ₯Ό μ λμ μΌλ‘ λ³κ²½νλ κ²μ
λλ€. μ΄κΈ° μμ 볡μ¬λ μ κ°μ²΄λ₯Ό λ§λ€μ§λ§ μ κ°μ²΄λ μλ³Έκ³Ό λμΌν byId
λ° ids
μ°Έμ‘°λ₯Ό κ°λ¦¬ν΅λλ€. λ°λΌμ byId[action.id] = action.value
λ° ids.push(action.id)
νλ©΄ μλ νλͺ©μ΄ λ³κ²½λ©λλ€.
νμμ μλ μνν΄μΌ νλ μμ μ λλ€. λΆλͺ ν, νλͺ©μ μ€μ²©νλ κ²½μ° λ€μ μ₯ν©ν΄μ§ μ μμ΅λλ€. μΆμνλ₯Ό μλνλ μ¬λ¬ μ νΈλ¦¬ν° λΌμ΄λΈλ¬λ¦¬κ° μμΌλ©° λ΄ Redux λΌμ΄λΈλ¬λ¦¬ λͺ©λ‘μ λΆλ³ λ°μ΄ν° νμ΄μ§μ κ·Έ μ€ μΌλΆκ° λμ΄λμ΄ μμ΅λλ€.
Redux FAQμλ λ³κ²½ λΆκ°λ₯νκ² μνλ₯Ό μ¬λ°λ₯΄κ² μ λ°μ΄νΈνλ λ°©λ²μ λν μ§λ¬Έλ μμ΅λλ€. http://redux.js.org/docs/FAQ.html#react -not-rerendering .
νμμ μλ μνν΄μΌ νλ μμ μ λλ€. λΆλͺ ν, νλͺ©μ μ€μ²©νλ κ²½μ° λ€μ μ₯ν©ν΄μ§ μ μμ΅λλ€. μΆμνλ₯Ό μλνλ μ¬λ¬ μ νΈλ¦¬ν° λΌμ΄λΈλ¬λ¦¬κ° μμΌλ©° λ΄ Redux λΌμ΄λΈλ¬λ¦¬ λͺ©λ‘μ λΆλ³ λ°μ΄ν° νμ΄μ§μ κ·Έ μ€ μΌλΆκ° λμ΄λμ΄ μμ΅λλ€.
λν κ°μκΈ° ꡬμ±μ΄ λμμ΄ λ©λλ€.
function byId(state = {}, action) {
switch (action.type) {
case 'ADD':
return {
...state,
[action.id]: action.value
}
default:
return state
}
}
function ids(state = [], action) {
switch (action.type) {
case 'ADD':
return [ ...state, action.id ]
default:
return state
}
}
const reducer = combineReducers({
byId,
ids
})
@gaearon @markerikson κ°μ¬ν©λλ€. λμ μΈλ±μ€ λͺ©λ‘(IDλ³)μ κ΄λ¦¬νλ κ³ μ°¨ κ°μκΈ°λ₯Ό μμ±νκ² λμμ΅λλ€.
μ½λ: https://gist.github.com/elado/95484b754f31fcd6846c7e75de4aafe4
κ°μ₯ μ μ©ν λκΈ
λν κ°μκΈ° ꡬμ±μ΄ λμμ΄ λ©λλ€.