Replies: 2 comments 1 reply
-
You can create an effect in your ComponentStore. Below is an example. Note: class ChartDynamicCompanyCountStore extends ComponentStore<State> {
private readonly authStore = inject(AuthStore);
loadChartData = this.effect(() => {
const companyIdChange$ = toObservable(authStore.companyId);
return companyIdChange$.pipe(
tap(companyId => {
const request: DynamicCompanyCountRequest = {
name: "CompanyCount",
startDate: DateFormatting.fromDateTimeToString(
this.startDate(),
),
endDate: DateFormatting.fromDateTimeToString(this.endDate()),
};
this.load(request);
})
);
});
} |
Beta Was this translation helpful? Give feedback.
-
Hello, in a SignalStore, I wouldn't recommend you create an effect to manage data changes. As it is mentioned in Angular docs https://angular.dev/guide/signals#use-cases-for-effects. I would rather do similar as you did with toObservable: export class ChartDynamicPracticeCountComponent {
store = inject(ChartDynamicCompanyCountStore);
authStore = inject(AuthStore);
public constructor() {
toObservable(
this.authStore.companyId,
)
.pipe(takeUntilDestroyed())
.subscribe(() => {
this.store.load({
name: "CompanyCount",
startDate: DateFormatting.fromDateTimeToString(
this.store.startDate(),
),
endDate: DateFormatting.fromDateTimeToString(this.store.endDate()),
});
});
}
} Or something like this https://ngrx.io/guide/signals/rxjs-integration#handling-api-calls where I have rxMethod within your ChartDynamicCompanyCountStore with some input, and then in your component, you call the rxMethod with the this.authStore.companyId signal, so every time the signal changes, the rx method is called. |
Beta Was this translation helpful? Give feedback.
-
I have what seems to be a simple scenario where I have a global store AuthStore that contains a signal for the current selected CompanyId which can be switched by the user via the UI at any given point. I then have a chart component that needs to refresh it's data if the CompanyId signal changes via a rxMethod method that makes a HTTP request to retrieve the new data.
The only solution I've found is to convert my signal to an Observable and subscribe to it as shown below. However, I was hoping that subscribing and unsubscribing in my components might become a thing of the past.
When the user changes the company I need to refresh the chart data by calling loadChartData, this was all perfectly feasible using the Redux pattern with @ngrx/store via side effects, actions and reducers. Have I missed something or is my design just simply not conducive to this approach?
Beta Was this translation helpful? Give feedback.
All reactions