Adaptable

#State

Adaptable keeps everything that describes the grid in a single state object: the available columns, the views (column order, sorting, grouping, pivoting, aggregations, filters, selection), the dashboard, styled cells, flashing cells, alerts, cell notes, the theme and more. Each module owns one key — state.view, state.dashboard, state.styledCell, state.alert, state.cellNote and so on.

Note

State describes how the grid presents data. It never contains the data itself — rows come from the data prop (see Local & remote data).

Only view is required — you need at least one view. Every other slice is optional and falls back to the module defaults. The full shape, slice by slice, is in the state reference.

#Default state

Pass the initial state through the defaultState prop. Adaptable owns the state from then on: every user interaction — resizing, reordering or sorting columns, grouping, switching views, selecting rows — updates it internally.

<Adaptable.Provider
  adaptableId="my-grid"
  primaryKey="id"
  data={data}
  defaultState={defaultState}
>
  <Adaptable.Workspace />
</Adaptable.Provider>
#
Passing a default state

The default state defines two views over the same columns: a flat one and one grouped by language. Switch views from the picker in the dashboard, sort or resize a column, then reload the page — your changes are still there.

open
loading demo…

The primaryKey names the field that uniquely identifies each row. Row and cell selection, editing and many other features rely on it, so make sure the values are unique. The field doesn't need a column of its own.

Views and columns are covered in Views; the getting started page walks through a first defaultState.

#Persistence

By default, Adaptable persists the state to localStorage, under the stateKey prop — or the adaptableId when you don't pass stateKey. On the next mount it loads the saved state and applies it on top of your defaultState.

What gets saved is not the whole state but a JSON patch: the difference between your defaultState and the current state. A user who only switched views stores something like:

[{ "op": "replace", "path": ["view", "currentViewId"], "value": "by-language" }]

Functions (renderers, value getters, sort comparators) are never serialized — they are restored from your defaultState when the patch is applied. If a stored patch no longer fits your defaultState (for example after you removed a view it refers to), Adaptable logs a warning and starts from the defaultState.

Warning

State persistence is turned off when no valid license key is found.

#Custom persistence

To store state elsewhere — typically your server — pass a persistState function, paired with a loadState function that does the reverse:

const persistState: AdaptableProps['persistState'] = async ({
  state,
  stateKey,
}) => {
  await fetch(`/api/grid-state/${stateKey}`, {
    method: 'PUT',
    body: JSON.stringify(state),
  });
};

const loadState: AdaptableProps['loadState'] = async ({ stateKey }) => {
  const response = await fetch(`/api/grid-state/${stateKey}`);
  return response.ok ? response.json() : null;
};

<Adaptable.Provider
  adaptableId="my-grid"
  primaryKey="id"
  data={data}
  defaultState={defaultState}
  persistState={persistState}
  loadState={loadState}
>
  <Adaptable.Workspace />
</Adaptable.Provider>;

Both functions receive a context with stateKey, adaptableId and adaptableApi; persistState also gets state — the patch to store. persistState runs after every state change, so debounce or batch it on your side if your backend needs that. loadState runs once, on mount, and resolves to whatever persistState last stored (or null when there is nothing).

Tip

Keep persistState stable — define it outside the component or wrap it in useCallback. Passing a new function triggers another save.

#
Custom persistState and loadState

The state is saved under a custom localStorage key, with a short delay standing in for a network request. Sort, resize or reorder columns and watch the persisted patch update below the grid.

open
loading demo…

Loading state is always asynchronous. When data is a function, the grid waits for the saved state before its first fetch, so that fetch already uses the restored view, sorting and grouping — see Data loading waits for state hydration.

#Listening to state changes

onStateChange is called with the new and the previous state after every change:

<Adaptable.Provider
  {...props}
  onStateChange={(state, prevState) => {
    console.log('state changed', state);
  }}
/>

It doesn't tell you what changed. To react to one part of the state, use onStateChangeWithSelectors — an array of selector / callback pairs. The callback runs only when the value returned by its selector changes, and receives the new value, the previous value and a context with adaptableApi. Wrap each pair in the withState helper to get the callback typed from the selector:

const listeners = [
  withState({
    selector: (state) => state.view.currentViewId,
    callback: (currentViewId, previousViewId) => {
      console.log(`view changed from ${previousViewId} to ${currentViewId}`);
    },
  }),
];

<Adaptable.Provider {...props} onStateChangeWithSelectors={listeners} />;

A pair can also carry an equals(a, b) function, when the default comparison isn't what you need.

Tip

Pass a stable array (define it outside the component or memoize it): every new array re-subscribes all the listeners.

#
Listening to state changes

onStateChange counts every change, while two selectors track the current view and its sorting. Switch views and click column headers to sort.

open
loading demo…

Inside the Provider, components can read the state with hooks instead: useAdaptableState selects a slice and re-renders on change, useAdaptableStateSubscribe subscribes without re-rendering, and useCurrentViewState returns the current view.

#Controlled state

To own the state yourself, pass state instead of defaultState, together with onStateChange:

const [state, setState] = useState<AdaptableUserState>(initialState);

<Adaptable.Provider
  adaptableId="my-grid"
  primaryKey="id"
  data={data}
  state={state}
  onStateChange={setState}
>
  <Adaptable.Workspace />
</Adaptable.Provider>;

The grid renders whatever state you pass. User interactions don't change it directly — they call onStateChange with the next state, and the grid updates once you pass that state back.

Warning

Without onStateChange, a controlled grid ignores user interactions. Also pick one mode per instance: switching between state and defaultState on a mounted grid recreates it and logs a warning.

Controlled state lets you drive the grid declaratively: pass a new state to switch the view, reorder columns or change the sorting — no API calls needed. Persistence still applies: on mount, the saved state is loaded and handed to you through onStateChange.

#
Controlling the state

The buttons change the current view and the sorting by writing to the React state that feeds the grid's state prop. Changes made in the grid itself flow back through onStateChange.

open
loading demo…