[英]What is the proper way of rendering a component passed as a prop? And how to extend it?
[英]What is the proper way to have a copy of redux state on a component prop
我有这样的事情:
ngOnInit() {
this.reduxServiceHandle = this.reduxService.subscribe(() =>
this.reduxUpdates()
);
}
reduxUpdates(): void {
const newState: TransportationControlTowerState = this.reduxService.getState();
// Check if feature groups object has changed
if (!isEqual(this.groups, newState.groups)) {
...
this.groups = newState.groups;
}
}
这是我的减速机:
case TransportationControlTowerActions.ADD_GROUP:
return newState = {
...state,
groups: { ...state.groups, [payload.groupId]: payload.group }
};
break;
所以,我的问题是:在将它保存在this.groups prop之前,我是否需要克隆该状态? this.groups = newState.groups;
我认为每次更改redux状态时,都会返回一个新的状态对象,因此我的本地状态(this.groups)指向旧状态不会出现问题。
但我只是想确保我没有遵循任何反模式。
问候,
官方Redux文档说:
State是只读的
改变状态的唯一方法是发出一个动作,一个描述发生的事情的对象。
这可以确保视图和网络回调都不会直接写入状态。 相反,他们表达了改变国家的意图。
您可以在此处查看核心原则的完整列表https://redux.js.org/introduction/three-principles
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.