gpt4 book ai didi

angular - 寻找使用 ngrx 8 沉浸式制作的示例

转载 作者:搜寻专家 更新时间:2023-10-30 21:21:01 24 4
gpt4 key购买 nike

环顾网络,找不到使用的例子从“immer”导入产品,{Draft};使用 ngrx on()我能找到的最接近的是:一个不完整的解决方案: https://github.com/immerjs/immer/issues/252

import produce, { Draft } from 'immer';
import { ActionCreator, createReducer, on } from '@ngrx/store';
import { ActionType, FunctionWithParametersType } from '@ngrx/store/src/models';

function produceOn<Type extends string, C extends FunctionWithParametersType<any, object>, State>(
actionType: ActionCreator<Type, C>,
callback: (draft: Draft<State>, action: ActionType<ActionCreator<Type, C>>) => any,
) {
return on(actionType, (state: State, action): State => produce(state, (draft) => callback(draft, action)));
}

// Usage:

const featureReducer = createReducer(
initialState,
produceOn(action, (draft, action) => {
// TODO STUFF
}
);

export function reducer(state = initialState, action) {
return featureReducer(state, action);
}

不确定如何将 on() 与 immer 与 ngrx 8 结合

肖恩。

最佳答案

找到答案:

import {createReducer} from '@ngrx/store';
import {on} from "@ngrx/store";
import produce, {Draft} from "immer";

export const initialUserState: IUserState = {
knownUsers: [user1, user2],
selectedUser: null,
scenes: null
};

export function produceOn<Type extends string, C extends FunctionWithParametersType<any, object>, State>(
actionType: ActionCreator<Type, C>,
callback: (draft: Draft<State>, action: ActionType<ActionCreator<Type, C>>) => any,
) {return on(actionType, (state: State, action): State => produce(state, (draft) => callback(draft, action)));}

export const loadRequest = createAction('[Scenes API] Scene Load Request', props<{ businessId: BusinessId }>());
export const loadSuccess = createAction('[Scenes API] Scene Load Success', props<{ scenes: List<SceneModel> }>());

// ngrx 8+ with immer and support for on() within reducer

const featureReducer = createReducer(
initialUserState,
produceOn(loadSuccess, (draft, action) => {
draft.scenes = {myList: [1,2,3]};
}),
produceOn(loadFailure, (draft, action) => {
draft.scenes = {myList: []};
console.log('error loading...');
})
);

关于angular - 寻找使用 ngrx 8 沉浸式制作的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com