> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: onLayerStateChange()

## Article: onLayerStateChange()

## Article Link: https://dev.wix.com/docs/sdk/host-modules/dashboard/on-layer-state-change.md

## Article Content:

# onLayerStateChange()

Registers a `layerStateChange` event handler for a dashboard page, modal, or plugin [extension](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/dashboard-extensions/about-dashboard-extensions.md). This allows you to refresh a component's data when it becomes active, or pause resource intensive operations when the component is running in the background.

`onLayerStateChange()` accepts a callback that's triggered whenever the extension moves from the foreground to the background, or vice versa. For example, this can happen when a Wix user opens or closes a [modal](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/dashboard-extensions/dashboard-modals/about-dashboard-modals.md).

## Method declaration

```ts
(callback: (state: 'foreground' | 'background') => void) => { remove: () => void; };
```

## Parameters

| Name       | Type                     | Description                                                                                                                                                            |
|:-----------|:-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `callback` | Function | Called when the `layerStateChange` event fires. The function receives the extension's new layer state, `"foreground"` or `"background"`, as an argument. |

## Returns
```ts
{ remove: () => void; }
```

An object containing a method called `remove()`. Calling this method removes the `onLayerStateChange` event handler. 

## Examples

> **Note:** To call this method in [self-managed apps](https://dev.wix.com/docs/sdk/articles/get-started/about-self-hosted-apps.md), you need to create a [client](https://dev.wix.com/docs/sdk/articles/work-with-the-sdk/about-the-wix-client.md). See the [setup](https://dev.wix.com/docs/sdk/host-modules/dashboard/introduction.md#setup) guide for more details.

### Refresh data when component comes to the foreground
> **Note:** This example calls [`dashboard.onBeforeUnload()`](https://dev.wix.com/docs/sdk/host-modules/dashboard/on-before-unload.md) to demonstrate use of the `remove()` function.

```js
import { dashboard } from '@wix/dashboard';
import { refreshData } from './data-retrieval.js'

const { remove } = dashboard.onLayerStateChange((state) => {
  if (state === 'foreground') {
    refreshData();
  }
});

// Remove the onLayerStateChange handler when the `beforeUnload` event is triggered.
dashboard.onBeforeUnload(() => {
  remove();
})
```