如何在reducer中更新嵌套的immutable?

时间:2017-02-28 17:06:51

标签: reactjs redux immutable.js

我是immutable.js的新手。 我想更新home.filmList[0].isDetailModalShow = false. 我该怎么做? 请帮我! 我的状态是这样的:

{
  home: {
    filmList: [
      {
        id: 1,
        name: '111',
        imgUrl: '111',
        isDetailModalShow: false
      },
      {
        id: 2,
        name: '222',
        imgUrl: '',
        isDetailModalShow: false
      },
    ]
  }
}

和我的减速机一样

import { createReducer } from 'redux-act'
import { fromJS } from 'immutable'
import { setFilmList, toggleDetailModal } from './actions'

const initialState = fromJS({
  filmList: [],
})

export default createReducer({
  [toggleDetailModal]: (state, payload) => {
    const index = state.get('filmList').findIndex(item => item.id === payload)
    // state.get(filmList) is filmList: [........]
    // below line code do not work!
    return state.updateIn(['filmList', index, 'isDetailModalShow'], value => !value)
  },
}, initialState)

2 个答案:

答案 0 :(得分:1)

您可以使用不可变的setIn功能:

state.setIn(['filmList', 0, 'isDetailModalShow'], false)

答案 1 :(得分:0)

我找到了解决方案......

当我设置// old [setFilmList]: (state, payload) => { return state.set('filmList', payload) } // new, it works! [setFilmList]: (state, payload) => { return state.set('filmList', fromJS(payload)) }

我忘记了使用fromJS



app:tabBackground="?attr/transparentColor"
android:background="?attr/transparentColor"