As I delve into learning and utilizing Signals within Angular, I find it to be quite exhilarating. However, I have encountered some challenges in certain scenarios. I am struggling to come up with an effective approach when dealing with a component that has input signals, and I need to trigger a re-fetch of data whenever the input value changes.
Using computed
is not feasible as it cannot handle asynchronous operations. The effect
method, as per the documentation, is not meant to alter component state. Additionally, ngOnChanges is on its way to being deprecated in favor of Signals-based components and a zoneless approach.
Let's take a look at the following component:
@Component()
export class ChartComponent {
dataSeriesId = input.required<string>();
fromDate = input.required<Date>();
toDate = input.required<Date>();
private data = signal<ChartData | null>(null);
}
When any of the input signals receives a new value, I need to initiate a data re-fetch and update the private data
signal. How should I tackle this issue? What would be considered best practice in this scenario? Should I use the effect
method and disregard the guideline against modifying state?