使用angular redux,如何从数组中存在的元素中选择状态切片?
我正在使用angular redux创建一个商店。商店有一个名为“roomListState”的切片,它是一个房间数组。我有一个容器组件订阅此列表,并创建列表中每个房间的视图,这些房间的“打开”属性设置为true 当房间中的消息有更新时,我使用下面显示的reducer函数更新存储。容器组件订阅房间列表的更新。它将列表(带有rs.open属性)过滤到其开放房间列表中。无论何时更改任何房间,都会更新开放房间的容器构件列表,从而重新绘制所有开放房间 有没有更好的方法来显示开放式房间,而不必重新绘制所有房间? 作为重新创建所有房间的副作用,用户发送消息的房间正在失去焦点。使用angular redux,如何从数组中存在的元素中选择状态切片?,angular,redux,Angular,Redux,我正在使用angular redux创建一个商店。商店有一个名为“roomListState”的切片,它是一个房间数组。我有一个容器组件订阅此列表,并创建列表中每个房间的视图,这些房间的“打开”属性设置为true 当房间中的消息有更新时,我使用下面显示的reducer函数更新存储。容器组件订阅房间列表的更新。它将列表(带有rs.open属性)过滤到其开放房间列表中。无论何时更改任何房间,都会更新开放房间的容器构件列表,从而重新绘制所有开放房间 有没有更好的方法来显示开放式房间,而不必重新绘制所有
export interface IAppState {
userState: IUserState;
roomListState: IRoomListState;
}
export interface IRoomListState {
rooms: Array<Room>;
}
//...
// adds an array of messages to a room using a supplied id
function addMessagesToRoom(state: IRoomListState, action: IRoomListAction): IRoomListState {
let newState = { ...state };
let newRooms = [...state.rooms];
newState.rooms = newRooms;
let roomIndex = newRooms.findIndex((r: Room) => r.id === action.roomId);
if (roomIndex === -1) {
console.log('Error: room id not found for message update. State not updated');
return state;
} else {
newRooms[roomIndex] = new Room(state.rooms[roomIndex]);
let newMessages = [...newRooms[roomIndex].messages, ...action.messages];
newRooms[roomIndex].messages = newMessages;
return newState;
}
}
导出接口IAppState{
userState:IUserState;
roomListState:IRoomListState;
}
导出接口IRoomListState{
房间:阵列;
}
//...
//使用提供的id向文件室添加消息数组
函数addMessagesToRoom(状态:IRoomListState,操作:IRoomListAction):IRoomListState{
让newState={…state};
让newRooms=[…state.rooms];
newState.rooms=新房间;
让roomIndex=newRooms.findIndex((r:Room)=>r.id==action.roomId);
如果(roomIndex==-1){
console.log('错误:找不到用于消息更新的房间id。状态未更新');
返回状态;
}否则{
newRooms[roomIndex]=新房间(state.rooms[roomIndex]);
让newMessages=[…newRooms[roomIndex].messages,…action.messages];
newRooms[roomIndex]。消息=新消息;
返回新闻状态;
}
}
My container组件正在订阅状态的roomList部分的更新:
public ngOnInit() {
this.openRooms$ = this.ngRedux.select(['roomListState', 'rooms']).map((rs: Array<Room>) => {
let ors: Array<Room> = [];
rs.forEach((r: Room) => {
if (r.open) {
ors.push(r);
}
});
return ors;
});
this.openRooms$.subscribe((rs: Array<Room>) => {
this.openRooms = rs;
// console.log('Open Rooms List is now: ', this.openRooms);
})
public ngOnInit(){
this.openRooms$=this.ngRedux.select(['roomListState','rooms']).map((rs:Array)=>{
让OR:Array=[];
接待费((接待费:房间)=>{
如果(右打开){
ors.推(r);
}
});
返回器;
});
this.openRooms$.subscribe((rs:Array)=>{
此参数为:1.openRooms=rs;
//log('openRooms列表现在是:',this.openRooms);
})