#useAdaptableStateSubscribe
useAdaptableStateSubscribe() runs a callback whenever a part of the
Adaptable state changes. Unlike
useAdaptableState, it does not re-render
your component — use it for side effects: logging, analytics, syncing a value
to your server or to another part of your app.
import { AdaptableState, useAdaptableStateSubscribe } from '@adaptabletools/datagrid-react';
const selectCurrentViewId = (state: AdaptableState) => state.view.currentViewId;
function ViewChangeTracker() {
useAdaptableStateSubscribe({
selector: selectCurrentViewId,
callback: (viewId, oldViewId, { adaptableApi }) => {
console.log('view changed from', oldViewId, 'to', viewId);
},
});
return null;
}#Parameters
The hook 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 renewed whenever selector, callback or equals
change identity. Declare the selector outside the component (or memoize it) to
keep a single subscription.
To listen from outside React, pass the same { selector, callback, equals }
objects to the onStateChangeWithSelectors prop, or
use onStateChange 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.