-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
access store from effects in WithRedux #35
Comments
@Chudroy I'm not sure if that's technically possible. We are within a signalFeature and have, therefore, no access to the final store. Could you describe your use case maybe so that I get a better understanding? |
hi, sure! for example, I used to have a withMethods in my signal store, that looked like this ...
withMethods(
(
store,
campaignsService = inject(CampaignsService),
...
) => {
... and a function within this that was like so: const loadMachineData = rxMethod<{
page: number;
pageSize: number;
order?: { field: string; order: string };
}>(
pipe(
tap(() => patchState(store, { isLoading: true })),
switchMap(({ page, pageSize, order }) => {
return campaignsService
.getMachineData(
store.selectedMachine(),
store.selectedCampaign(),
page,
pageSize,
order,
)
.pipe(
tapResponse(
(response) => {
....
},
(error: HttpErrorResponse) => {
...
},
),
);
}),
),
); Basically, what I was able to achieve here was call Now that i've refactored my signal store to use withRedux, in my effects, I can't access the store, so i have to select those pieces of state in the component, and pass them as arguments to the action that activates the effect: in the component: loadMachineData(): void {
const campaign = this.campaignsStore.selectedCampaign();
const machine = this.campaignsStore.selectedMachine();
if (!campaign || !machine) return;
const page = this.paginator.pageIndex + 1;
const pageSize = this.paginator.pageSize;
const order = { field: this.sort?.active, order: this.sort?.direction };
this.campaignsStore.loadMachineData({
campaign,
machine,
page,
pageSize,
order,
});
} and in the effect: loadMachineData$: create(actions.loadMachineData).pipe(
switchMap(({ campaign, machine, page, pageSize, order }) => {
return campaignsService
.getMachineData(machine, campaign, page, pageSize, order)
.pipe(
tapResponse(
(response) => {
actions.loadMachineDataSuccess({
data: response?.data || [],
machine,
campaign,
});
},
(error: HttpErrorResponse) => {
httpErrorSnackbarService.showHttpErrorSnackbar(error);
actions.loadMachineDataFailure({ error });
},
),
);
}),
), how i think it could be better: in component: loadMachineData(): void {
const page = this.paginator.pageIndex + 1;
const pageSize = this.paginator.pageSize;
const order = { field: this.sort?.active, order: this.sort?.direction };
this.campaignsStore.loadMachineData({
page,
pageSize,
order,
});
} adding a effects(actions, create, store) {
loadMachineData(): void {
const page = this.paginator.pageIndex + 1;
const pageSize = this.paginator.pageSize;
const order = { field: this.sort?.active, order: this.sort?.direction };
this.campaignsStore.loadMachineData({
store.selectedMachine(),
store.selectedCampaign(),
page,
pageSize,
order,
});
}
...
} On a side-note, I've researched accessing store from effects, and it seems to be a common practice, see https://stackoverflow.com/questions/49177620/bad-practice-to-use-store-in-effect
|
hi, any thoughts or news on this issue? |
Yes, we see the requirement for it and will be made available. I'm currently stuck on #44, but maybe this one here should get higher priority. |
A quick update on that one. In order to avoid having too many breaking changes, I'd like to introduce this new feature for the new actions. We got some feedback from the NgRx team, that we shouldn't have the actions defined in Another side effect will be that the actions will not add methods to the signalStore anymore but need to be dispatched. We are checking if that requires some kind of In order to get access to the store:
@ngaritagoitia, @Chudroy, what is your opinion on that? |
Looks good, component store can communicate with global store in a better way, and i understand this also provides access to the store from the effects, so cool. Looking forward to the update! |
bumping - looking forward for the update as well. |
as of right now i'm not seeing a way to access state of a signal store from effects in using WithRedux. Would it be possible to add a parameter to effects to make the store accessible? something like
The text was updated successfully, but these errors were encountered: