> To build a Wix site agentically, or add Wix features like bookings or a store to a site you already have, follow https://www.wix.com/headless/skill.md
> 
> 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: Use a Data Source in an Editor React Component in Studio Two

## Article: Use a Data Source in an Editor React Component in Studio Two

## Article Link: https://dev.wix.com/docs/studio-two/code-your-site/code-model/editor-react-components/use-a-data-source-in-an-editor-react-component-in-studio-two.md

## Article Content:

# Use a Data Source in an Editor React Component in Studio Two

An [Editor React Component (ERC)](https://dev.wix.com/docs/studio-two/code-your-site/code-model/editor-react-components/about-editor-react-components.md) can use a Data Source you built on your site by calling the Data Source's React hook in its own code. The hook returns the Data Source's current values and its functions, such as a count and an `increment` function.

A Wix user usually connects an ERC's props to a Data Source in the editor. Calling the hook in the ERC's code instead lets the ERC read every value and call every function the Data Source exposes, without the Wix user connecting each one. The ERC then works only inside a page, container, or section that has the Data Source attached. To compare this approach with editor binding, see [Component Integration Patterns](https://dev.wix.com/docs/studio-two/code-your-site/code-model/component-integration-patterns.md#call-a-contexts-hook-in-an-ercs-code).

Follow the instructions below to:

1. Check the Data Source's module specifier.
2. Declare the Data Source in the manifest.
3. Use the Data Source in your component.
4. Build the component and regenerate the manifest.
5. Check the component in the editor.
6. Publish your site and check it.

## Before you begin

Make sure that:

- You have a Data Source in your site project. To build one, see [Build a Data Source in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/data-sources/build-a-data-source-in-studio-two.md).
- You have an ERC in your site project. To add one, see [Add an Editor React Component Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/editor-react-components/add-an-editor-react-component-extension-in-studio-two.md).

## Step 1 | Check the Data Source's module specifier

Your ERC imports the Data Source's hook from its module specifier. The module specifier is set in `resources.contextSpecifier.moduleSpecifier` in the Data Source's `<provider-folder>.extension.ts` file, under `src/extensions/site/contexts/<provider-folder>/`. The hook's name is in `resources.contextSpecifier.hook`.

For example:

```ts
contextSpecifier: {
  hook: 'useCounterContext',
  moduleSpecifier: '@/my-data-source',
},
```

The generated manifest has no module specifier. If yours doesn't have one yet, add it as described in [Build a Data Source in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/data-sources/build-a-data-source-in-studio-two.md#step-2--declare-the-context-provider-contract).

## Step 2 | Declare the Data Source in the manifest

Declare the Data Source in your ERC's manifest before you import its hook. The declaration lets your component build with the hook's import, and tells the site to load the Data Source for your component.

To declare the Data Source:

1. In the **Code** tab, open your ERC's `<component-folder>.extension.ts` file under `src/extensions/site/components/<component-folder>/`.
2. In both `resources.client` and `resources.editor`, add a `dependencies` object with a `contextDependencies` array. `resources.client` covers the live site, and `resources.editor` covers the editor.
3. Add the Data Source's module specifier to the array, for example `'@/my-data-source'`.
4. Save the file.

For example:

```ts
resources: {
  client: {
    componentUrl,
    dependencies: {
      contextDependencies: ['@/my-data-source'],
    },
  },
  editor: {
    componentUrl: componentPreviewUrl,
    dependencies: {
      contextDependencies: ['@/my-data-source'],
    },
  },
},
```

<blockquote class="important">

__Important:__ Without the declaration, the build fails with an error like `Rolldown failed to resolve import "@/my-data-source"`. If you see this error, add the module specifier to `contextDependencies` and build again.

</blockquote>

For the other fields in `resources`, see [About the Editor React Component Manifest](https://dev.wix.com/docs/site-extensions/editor-react-components/manifest-reference/about-the-editor-react-component-manifest.md).

## Step 3 | Use the Data Source in your component

To use the Data Source:

1. Open your ERC's `<component-folder>.tsx` file.
2. Import the hook from the Data Source's module specifier.
3. Call the hook at the top level of your component, and destructure the values and functions you need.
4. Use the values in your JSX, and call the functions from event handlers.
5. Save the file.

When a value in the Data Source changes, the hook returns the new value and your component renders again.

The following component shows the count from the counter Data Source and a button that increments it:

```tsx
import React from 'react';
import type { FC } from 'react';
import clsx from 'clsx';
import { useCounterContext } from '@/my-data-source';
import styles from './counter-display.module.css';
import type { CounterDisplayProps } from './counter-display.props';

const CounterDisplay: FC<CounterDisplayProps> = ({ id, className, title }) => {
  const { count, increment } = useCounterContext();

  return (
    <div id={id} className={clsx('counter-display', styles.counterDisplay, className)}>
      <h2 className={clsx('title', styles.title)}>
        {title}: {count}
      </h2>
      <button className="button" onClick={() => increment()}>
        +1
      </button>
    </div>
  );
};

export default CounterDisplay;
```

The component still follows the rules for every ERC, such as passing `id` and `className` to its root element. For those rules, see [Build an Editor React Component in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/editor-react-components/build-an-editor-react-component-in-studio-two.md).

## Step 4 | Build the component and regenerate the manifest

To build the component and regenerate the manifest:

1. In the **Code** tab, open the terminal. Press `Control+J`, or `Command+J` on macOS.
2. Run:

   ```bash
   npx wix build && npx wix generate manifest
   ```

The manifest generator renders your component with a sample version of each Data Source on your site, so regenerating works even though the component calls the hook. For what the generated manifest contains, see [Build an Editor React Component in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/editor-react-components/build-an-editor-react-component-in-studio-two.md#step-5--regenerate-and-check-the-manifest).

## Step 5 | Check the component in the editor

Place the ERC inside an area that has the Data Source attached, and check that it shows the Data Source's values.

To check the component:

1. Switch to the **Design** tab.
2. In the **Layers** panel, select the page, container, or section where you want to use the component. Then select **More Actions > Connect to Data**.
3. Click **Connect Source**, then select your Data Source under **Custom code**. For more about attaching a Data Source, see [Build a Data Source in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/data-sources/build-a-data-source-in-studio-two.md#step-5--check-the-data-source).
4. Navigate to **Add Elements > AI Generated**, and select your component to place it inside that area. If it isn't listed, ask **Aria**, Wix's AI assistant, to place it for you. For how to work with Aria, see [Working with Aria](https://support.wix.com/en/article/wix-studio-20-working-with-aria).
5. Check that the component shows the Data Source's values.
6. Click **Preview**, and check that the component's functions work.

For the example, check that the component shows **Title: 0**. In **Preview**, check that clicking **+1** increases the count.

<blockquote class="tip">

**Tip:** If the editor shows an error instead of your component, the ERC isn't inside an area with the Data Source attached, so the hook throws. Move the ERC into that area, or attach the Data Source to the page, container, or section that contains the ERC.

</blockquote>

## Step 6 | Publish your site and check it

When you're ready, click **Publish** in the Studio Two editor. Your ERC and your Data Source ship with the published site, and the ERC reads the Data Source wherever it's placed inside an area with the Data Source attached.

On the live site, check that the component works the same way it did in **Preview**.

## See also

- [Build a Data Source in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/data-sources/build-a-data-source-in-studio-two.md)
- [Use a Wix Context in an Editor React Component in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/editor-react-components/use-a-wix-context-in-an-editor-react-component-in-studio-two.md)
- [Component Integration Patterns](https://dev.wix.com/docs/studio-two/code-your-site/code-model/component-integration-patterns.md)
- [About Data Sources](https://dev.wix.com/docs/studio-two/code-your-site/code-model/data-sources/about-data-sources.md)