> For the complete documentation index, see [llms.txt](https://docs.sealights.io/knowledgebase/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/browser-agent/framework-setups/react.md).

# React

> **Applies to:** React applications built with Vite or Create React App (react-scripts). For a custom webpack, esbuild, Rsbuild or Rollup setup, see that bundler's page; the React-specific notes below still apply. For Next.js, see [Next.js](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/node.js-agent/advanced-features/next.js-with-sealights-node-and-browser-agents-multi-module-scan.md).

| Toolchain        | Source maps                                               | Output to scan | `--es6Modules` |
| ---------------- | --------------------------------------------------------- | -------------- | -------------- |
| Vite             | Off by default; turn on `build.sourcemap`                 | `dist`         | Yes            |
| Create React App | On by default (`GENERATE_SOURCEMAP=false` turns them off) | `build`        | No             |

## React with Vite <a href="#vite" id="vite"></a>

**1. Source maps:**

```js
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  build: { sourcemap: true },   // 'hidden' also works: the .map files stay next to the bundles
});
```

**2. Build, scan and instrument:**

```bash
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npm ci
npx vite build

npx slnodejs config --tokenFile sltoken.txt --appName my-react-app \
  --branchName "$BRANCH_NAME" --buildName "$BUILD_NUMBER"
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
  --scanDir dist --scmType git --es6Modules \
  --instrumentForBrowsers --outputPath sl_dist \
  --labId qa-env --excludeSourceMapFiles skipCopy
```

Deploy the contents of `sl_dist` in place of `dist`.

## React with Create React App <a href="#cra" id="cra"></a>

**1. Source maps:** on by default. Make sure the CI build does not set `GENERATE_SOURCEMAP=false`.

**2. Build, scan and instrument:**

```bash
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npm ci
npm run build

npx slnodejs config --tokenFile sltoken.txt --appName my-react-app \
  --branchName "$BRANCH_NAME" --buildName "$BUILD_NUMBER"
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
  --scanDir build --scmType git \
  --instrumentForBrowsers --outputPath sl_build \
  --labId qa-env --excludeSourceMapFiles skipCopy
```

Deploy the contents of `sl_build` in place of `build`.

## React-specific notes <a href="#notes" id="notes"></a>

| Topic                                                     | What to do                                                                                                                                                                                                                                                                                                                                                                                                           |
| --------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| React itself                                              | `react`, `react-dom`, `react-is`, `scheduler` and `prop-types` inside your bundles are not instrumented by default.                                                                                                                                                                                                                                                                                                  |
| UI libraries (MUI, Ant Design, ...) inside your bundles   | List them in a `.slignore.instrument` file to keep them out of coverage and reduce instrumented size. Your file **replaces** the built-in list, so keep the React entries in it: `react`, `react-dom`, `react-is`, `scheduler`, `prop-types`, `core-js`, `regenerator-runtime`, then add for example `@mui/` and `antd`.                                                                                             |
| Single-page app routing with `--splitPreambleIntoFile`    | React apps usually have no `<base href>`. Pass a full URL in `--preambleFileUrl` (for example `https://app.qa.example.com`), or deep routes such as `/orders/42` request the configuration file below the route and get a 404.                                                                                                                                                                                       |
| Lazy-loaded components (`React.lazy`, dynamic `import()`) | No extra setup.                                                                                                                                                                                                                                                                                                                                                                                                      |
| Scanning `src` JSX directly                               | Not needed; scan the build output. If you scan un-bundled JSX sources, add `--babylonPlugins jsx`.                                                                                                                                                                                                                                                                                                                   |
| Tests                                                     | Playwright or Cypress with the SeaLights plugin set the current test in the browser; see [Node.js Testing Plugins](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/node.js-testing-plugins.md). Jest unit tests: [Jest plugin](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/node.js-testing-plugins/jest-framework.md). |

## Check it <a href="#check" id="check"></a>

* Before CI: `npx slnodejs dryRun --scanDir dist --es6Modules --verbose --instrumentForBrowsers --tokenFile sltoken.txt` (CRA: `--scanDir build`, without `--es6Modules`). `sl-mapping.json` should resolve to `src/...` files.
* After deployment: [Check that it works](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/browser-agent/setting-up-browser-coverage.md#check-that-it-works).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/browser-agent/framework-setups/react.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
