#injectAdaptableStateSubscribe
injectAdaptableStateSubscribe() (the Angular counterpart of the
useAdaptableStateSubscribe hook) runs a callback whenever a part of the
Adaptable state changes. Unlike
injectAdaptableState, it returns nothing,
so nothing in your component is updated — use it for side effects: logging,
analytics, syncing a value to your server or to another part of your app.
Call it in an injection context (a field initializer or the constructor) of a
component rendered inside <adaptable-provider>:
import { Component } from '@angular/core';
import { injectAdaptableStateSubscribe } from '@adaptabletools/datagrid-angular';
@Component({
selector: 'view-change-tracker',
template: '',
})
export class ViewChangeTracker {
constructor() {
injectAdaptableStateSubscribe({
selector: (state) => state.view.currentViewId,
callback: (viewId, oldViewId, { adaptableApi }) => {
console.log('view changed from', oldViewId, 'to', viewId);
},
});
}
}#Parameters
The inject function takes one object:
callback—(value, oldValue, context) => void. Called when the value returned byselectorchanges, with the new and the previous value.context.adaptableApiis the Adaptable API.equals— optional(a, b) => boolean. Decides whether two selected values are equal. Defaults toObject.is, so pass one when your selector builds a new object or array each time.selector—(state: AdaptableState) => value. Picks the part of the state you are interested in.
The subscription is made with the selector and equals of the call, once
the provider is set up, and ends when the component is destroyed. There is
nothing to memoize: write the selector and the callback inline. The callback
can read and set your signals; it reads them when it runs.
To listen from outside a component, pass the same
{ selector, callback, equals } objects to the
onStateChangeWithSelectors input of
<adaptable-provider>, or use its
(stateChange) output
to be notified of every change.
Two subscriptions append to the log above the grid: one on
state.view.currentViewId, one on state.quickSearch.searchText.
Switch the view or type in the quick search box to see new entries.