2016-12-15 95 views
1

一個嵌套的狀態我有一個對象在我的狀態如下:更新的終極版

Exercise: { 
    id: 1, 
    question: '', 
    type: '', 
    Groups: [ 
     { 
     id: 1, 
     category: { 
      id: 1, 
      value: 'xxx', 
      color: 'xxx' 
     }, 
     groupParts: [ 
      { 
      id: 1, 
      Index: 7 
      }, 
      { 
      id: 2, 
      Index: 11 
      } 
     ] 
     } 
    ] 
    } 

我怎樣才能在ID更新指數值:2在減速?

這是我最後一次嘗試不更新的價值,但會在當前狀態的另一部分:

case CURRENT_WORD_INDEX_UPDATED: 
    const index=action.selectedWordIndex 
    return{...state,index:{...state.Groups[0].groupParts[1].index,in‌​dex},} 
+0

你可以發表你迄今嘗試過的嗎? – Pineda

+0

@Pineda我編輯了這個問題並添加了我的最後一次嘗試 –

+0

你可以在這裏檢查它(這裏是http://stackoverflow.com/questions/40990993/how-to-change-nested-object-without-mutating-it-在-終極版/ 40991604#40991604)。我認爲它會幫助你。 – duwalanise

回答

1

你可以利用immutability-helper更新嵌套狀態

import update from 'immutability-helper'; 
 

 
...... 
 

 
case CURRENT_WORD_INDEX_UPDATED: 
 
    const index=action.selectedWordIndex 
 
    return update(state, { 
 
     Groups: { 
 
     0: { 
 
      groupParts: { 
 
       0: { 
 
       Index: { 
 
        $set: index 
 
       } 
 
       } 
 
      } 
 
     } 
 
     } 
 
    })

+0

完美的作品,謝謝 –

+0

很高興有幫助 –