简体   繁体   English

在reducer redux中使用新内容更新数组

[英]update array with new content in reducer redux

I am still getting my head around functional programming and how to return a non mutated object from a reducer. 我仍然对函数式编程以及如何从化简器返回非突变对象的问题有所了解。

I am trying to replace the contents of an old object in a reducer, without mutating the old state. 我试图在化简器中替换旧对象的内容,而又不改变旧状态。

so if the old state was 所以如果旧状态是

 {
        Actors:"Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl"
        Awards:"Won 1 Oscar. Another 9 wins & 22 nominations."
        Country:"USA, UK"
        Director:"Tim Burton"
 }

and the new state is 而新的状态是

{
        Actors:"George Clooney"
        Awards:"Won 9 Oscars."
        Country:"USA"
        Director:"Quentin Tarantino"
 }

My reducer looks like this 我的减速机看起来像这样

function reducer(state = {}, action){
  switch(action.type) {
    case 'GET_MOVIE':
      return //new Array here that has not been mutatated
    default:
        return state;
  }
}

My payload looks like this 我的有效载荷看起来像这样

{
    Actors:"Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl"
    Awards:"Won 1 Oscar. Another 9 wins & 22 nominations."
    Country:"USA, UK"
    Director:"Tim Burton"
}

If all the values of your object are changing everytime, you can simply return that new payload as new state. 如果对象的所有值每次都在变化,则只需将新的有效负载作为新状态返回即可。 But if only some of the values are changing then You can use ES6 Object.assign or object-assign as npm module. 但是,如果仅某些值在更改,则可以将ES6 Object.assignobject-assign用作npm模块。

If all the values are changing everytime then, 如果所有值每次都在变化,

function reducer(state = {}, action){
   switch(action.type) {
    case 'GET_MOVIE':
      return action.payload;
    default:
        return state;
  }
}

If some of the values are changing then, 如果某些值正在更改,

function reducer(state = {}, action){
   switch(action.type) {
    case 'GET_MOVIE':
      // let say Actors field changed, then 
      return Object.assign({}, state, {Actors: "Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl" });
    default:
        return state;
  }
}

Simply use non-mutative functions and return the new data structure. 只需使用非变异函数并返回新的数据结构。

If your state is simply {a: 1, b: 2, c: 3} and you want it to become {a: 4, b: 5, c: 6} just return {a: 4, b: 5, c: 6}. 如果您的状态仅为{a:1,b:2,c:3},而您希望它变成{a:4,b:5,c:6},则只需返回{a:4,b:5,c: 6}。

function reducer(state = {a: 1, b: 2, c: 3}, action){
  switch(action.type) {
    case 'GET_MOVIE':
      return action.payload // assuming action.payload = {a: 4, b: 5, c: 6}
    default:
        return state;
  }
}

It gets more interesting when you want the new state = [{a:1,b:2,c:3},{a:4, b:5, c: 6}]. 当您想要新状态= [{a:1,b:2,c:3},{a:4,b:5,c:6}]时,它会变得更加有趣。

For a good list of mutative vs non-mutative functions, this is helpful: https://jeff.is/blog/mutative-vs-non-mutative-array-methods-in-js.html 有关可变和非可变功能的好列表,这很有用: https : //jeff.is/blog/mutative-vs-non-mutative-array-methods-in-js.html

I've also found React's immutability helpers nice for deeply nested structures. 我还发现React的不变性助手非常适合深度嵌套的结构。 They use a Mongo style syntax. 他们使用Mongo风格的语法。

I am a bit confused about why you need array operators when you're dealing with an object. 我有点困惑为什么在处理对象时需要数组运算符。 With this current state, you can do this one of two ways, depending on your build tools, polyfills, and/or target browsers. 在当前状态下,您可以采用两种方式之一,具体取决于您的构建工具,polyfill和/或目标浏览器。

Using Object.assign() 使用Object.assign()

function reducer(state = {}, action){
  switch(action.type) {
    case 'GET_MOVIE':
      return Object.assign({}, state, {
        Actors: action.Actors,
        Awards: action.Awards,
        Country: action.Country,
        Director: action.Director
      });
    default:
        return state;
  }
}

Or with spread operator ... 或与传播者...

function reducer(state = {}, action){
  switch(action.type) {
    case 'GET_MOVIE':
      return {
        ...state,
        Actors: action.Actors,
        Awards: action.Awards,
        Country: action.Country,
        Director: action.Director
      }
    default:
        return state;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM