简体   繁体   English

我如何将对象传递给对象的属性?

[英]How i can pass an object to an attribute of an object?

I have this code in reducer: 我在reducer中有这个代码:

export const getSerieSuccess = (state = INITIAL_STATE, action) => {
   return {
      ...state,
      isLoadding: false,
      serie: action.serie   //action.serie is a object with id, name, genre e status
   }
}

I want to know if i do serie: action.serie , i am passing a value or a reference. 我想知道我是否做了serie: action.serie ,我正在传递一个值或一个引用。 I did this like: 我这样做:

export const getSerieSuccess = (state = INITIAL_STATE, action) => {
const serie = {...action.serie};
   return {
      ...state,
      isLoadding: false,
      serie
   }
}

How wai is better working with functional programming? wai如何更好地使用函数式编程?

You can use both variants as long as the payload of the action is not further referenced and will not be mutated/changed. 只要操作的有效负载未被进一步引用且不会发生变化/更改,您就可以使用这两种变体。

Good : 好的

store.dispatch(new SomeAction({a: b: {
  c: 1234
}));

Bad (because you pass a reference and change it afterwards): 不好 (因为你传递了一个引用并在之后更改它):

let value = new SomeAction({a: b: {
  c: 1234
});
store.dispatch(value);
value.a.b.c = 44543; // bad

setTimeout(_ => (value.a.b.c = 5555), 5000); // bad

Note : if you want to make it really save (no reference passing) you could make a copy of the passed value inside of the action. 注意 :如果你想让它真正保存(没有引用传递),你可以在动作内部复制传递的值。

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

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