Updating nested data in redux store

后端 未结 2 1109
醉话见心
醉话见心 2020-11-28 21:55

What\'s the best/correct way to update a nested array of data in a store using redux?

My store looks like this:

{
    items:{
        1: {
                   


        
2条回答
  •  清酒与你
    2020-11-28 22:01

    React's update() immutability helper is a convenient way to create an updated version of a plain old JavaScript object without mutating it.

    You give it the source object to be updated and an object describing paths to the pieces which need to be updated and changes that need to be made.

    e.g., if an action had id and link properties and you wanted to push the link to an array of links in an item keyed with the id:

    var update = require('react/lib/update')
    
    // ...
    
    return update(state, {
      items: {
        [action.id]: {
          links: {$push: action.link}
        }
      }
    })
    

    (Example uses an ES6 computed property name for action.id)

提交回复
热议问题