Adaptable

#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.

Listening to view changes
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 by selector changes, with the new and the previous value. context.adaptableApi is the Adaptable API.
  • equals — optional (a, b) => boolean. Decides whether two selected values are equal. Defaults to Object.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.

Note

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.

#
Subscribing to state changes

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.

open
loading demo…