javascript - 如何获取在 entityAdapter.getInitialState() 中传递的可选对象属性?
问题描述
我初始化了 entityAdapter 的状态并将选择器声明为
export const devicesAdapter = createEntityAdapter<Device>({
})
export const initialDevicesState = devicesAdapter.getInitialState({
listLoading: false,
actionsLoading: false,
totalCount: 0,
groups: [] as Group[],
deviceForEdit: null as Device,
error: null,
});
export const {
selectAll: selectAllDevices,
selectById: selectDeviceById,
selectIds: selectDeviceIds,
selectEntities: selectDeviceEntities,
} = devicesAdapter.getSelectors((state: RootState) => state.devices);
现在,在使用 createAsyncThunk 创建的操作中,我正在尝试访问我商店中组的当前状态
const groups=store.getState().devices.groups;
哪个还没有工作,我认为从根存储组件访问状态是一种不好的方法?
有没有更好的方法从设备级别访问组状态,例如使用 devicesAdapter 的选择器?
解决方案
使用 thunkAPI 的 getState() 方法使用 createAsyncThunk 获取存储状态非常简单,这是async payloadCreator的默认第二个参数