#useAdaptableState
useAdaptableState() is a composable that gives you a ref of the
Adaptable state — or of a part of it — in any component rendered
inside <Adaptable.Provider>. Call it in <script setup> (or in setup()).
The ref follows the state: a template, a computed or a watch that reads it
is updated when the value changes.
import { useAdaptableState } from '@adaptabletools/datagrid-vue';To run code when the state changes (logging, syncing to a server), use useAdaptableStateSubscribe: its callback also gets the previous value and the Adaptable API.
#Usage
Call it without arguments to get the whole state:
const adaptableState = useAdaptableState();Or pass a selector function to pick the part you are interested in:
const currentViewId = useAdaptableState((state) => state.view.currentViewId);const theme = useAdaptableState((state) => state.theme.currentTheme);What you get is a read-only shallow ref (Readonly<ShallowRef<T>>). Read
.value in the script; the template unwraps it.
<script setup lang="ts">
import { computed } from 'vue';
import { useAdaptableState } from '@adaptabletools/datagrid-vue';
const currentViewId = useAdaptableState((state) => state.view.currentViewId);
const theme = useAdaptableState((state) => state.theme.currentTheme);
// in the script, read .value
const summary = computed(() => `${currentViewId.value} (${theme.value} theme)`);
</script>
<template>
<!-- in the template, the ref is unwrapped -->
<div>Current view: {{ currentViewId }}</div>
<div>{{ summary }}</div>
</template>Prefer a selector: without one, the ref gets a new value on every state change,
and everything that reads it is updated. The selector receives the full
AdaptableState; the slices are described in the
state reference. It is a plain function of the state,
called when the state changes: a ref read inside the selector is not tracked.
To combine the result with your own reactive values, use a computed that
reads the returned ref.
For common reads, the dedicated composables are shorter —
useCurrentViewState for the active view,
or the feature composables (useViews, useTheme, …), which
also return the setters.
The current view is read from the state in two places: the line above the grid
(view label and theme) and the header of the First name column (view id),
rendered through the column's renderHeader.
Switch the view from the dashboard to see both update.