Redux: 질문: λ¦¬λ“€μ„œμ˜ μƒνƒœμ—μ„œ λ‚΄λΆ€ 객체의 λŒμ—°λ³€μ΄

에 λ§Œλ“  2016λ…„ 04μ›” 15일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: reduxjs/redux

이것은 μœ νš¨ν•œ 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 λ³΄κΈ°λŠ” μ˜¬λ°”λ₯΄κ²Œ μ—…λ°μ΄νŠΈλ©λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

ν›„μžμ˜ μ˜ˆλŠ” μˆ˜ν–‰ν•΄μ•Ό ν•˜λŠ” μž‘μ—…μž…λ‹ˆλ‹€. λΆ„λͺ…νžˆ, ν•­λͺ©μ„ μ€‘μ²©ν•˜λŠ” 경우 λ‹€μ†Œ μž₯ν™©ν•΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€. 좔상화λ₯Ό μ‹œλ„ν•˜λŠ” μ—¬λŸ¬ μœ ν‹Έλ¦¬ν‹° λΌμ΄λΈŒλŸ¬λ¦¬κ°€ 있으며 λ‚΄ 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
})

λͺ¨λ“  3 λŒ“κΈ€

μ•„λ‹ˆμš”, 첫 번째 μ˜ˆλŠ” μƒνƒœλ₯Ό μ ˆλŒ€μ μœΌλ‘œ λ³€κ²½ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 초기 얕은 λ³΅μ‚¬λŠ” μƒˆ 개체λ₯Ό λ§Œλ“€μ§€λ§Œ μƒˆ κ°œμ²΄λŠ” 원본과 λ™μΌν•œ 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

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰