---
title: Preview Surveys with Runtime Variables
product: Survey Creator
description: Use variable presets to test survey logic that depends on application data. This JavaScript demo shows how to switch customer profiles and edit variable values in Survey Creator's Preview tab.
framework: React
source: https://surveyjs.io/survey-creator/examples/test-runtime-dependent-survey-logic/reactjs
index: https://surveyjs.io/survey-creator/examples/overview.md
---

# Preview Surveys with Runtime Variables (React)

Survey logic can depend on application data, such as a customer's plan or role. To test this logic, you can save sample values as named sets called **variable presets**. Use the selector at the bottom of Preview to switch between three customer profiles: "Basic member", "Premium member", and "Premium administrator". Click **Manage presets** to view, edit, add, or delete presets.

To configure presets, set the [`variablePresets`](/survey-creator/documentation/api-reference/icreatoroptions#variablePresets) property to an object with these properties:

- `definition`\
A survey JSON definition whose questions describe the variables. Question names identify variables, while question types, choices, and validators configure the preset editor. In this demo, a Dropdown question defines `customerTier` and a Yes/No question defines `isAccountAdmin`.
- `presets`\
An array of named variable presets. Each entry contains a unique `name`, an optional `description`, and a `variables` object that maps variable names to values.

Variables in survey expressions are referenced using curly brackets, just as question names. This demo uses `"enableIf": "{customerTier} = 'premium'"` to enable priority support and `"visibleIf": "{isAccountAdmin} = true"` to show the billing question. The variable definition also makes these variables available in visual condition editors.

Preset edits are kept in memory for the browser session. To save them, handle the [`onVariablePresetsChanged`](/survey-creator/documentation/api-reference/survey-creator#onVariablePresetsChanged) event and persist `options.variablePresets` when `options.reason === "edit"`.

Refer to the Code tab for the complete survey and variable preset definitions.

## Files

### `public/index.html`

```html
<link href="https://fonts.googleapis.com/css?family=Open%20Sans:400,600&display=swap" rel="stylesheet">
<!-- Uncomment the following lines to enable Ace Editor in the JSON Editor tab -->
<!-- 
<script src="https://unpkg.com/ace-builds/src-min-noconflict/ace.js"></script>
<script src="https://unpkg.com/ace-builds/src-min-noconflict/ext-searchbox.js"></script>
<script src="https://unpkg.com/ace-builds/src-min-noconflict/theme-clouds_midnight.js"></script>
-->

<div id="surveyCreatorContainer" style="position: absolute; height: 100%; width: 100%"></div>
```

### `src/survey_json.js`

```js
export const surveyJSON = {
  "title": "Support Request",
  "description": "Tell us how we can help you with your account.",
  "pages": [
    {
      "name": "supportRequest",
      "elements": [
        {
          "type": "comment",
          "name": "requestDetails",
          "title": "How can we help?",
          "isRequired": true
        },
        {
          "type": "boolean",
          "name": "prioritySupport",
          "title": "Would you like priority support?",
          "description": "Priority support is available on the Premium plan.",
          "enableIf": "{customerTier} = 'premium'"
        },
        {
          "type": "comment",
          "name": "billingDetails",
          "title": "Do you need help with billing?",
          "description": "Account administrators can include billing questions in their request.",
          "visibleIf": "{isAccountAdmin} = true"
        }
      ]
    }
  ]
};
```

### `src/variable_presets.js`

```js
export const variablePresets = {
  "definition": {
    "elements": [
      {
        "type": "dropdown",
        "name": "customerTier",
        "title": "Customer plan",
        "isRequired": true,
        "choices": [
          { "value": "basic", "text": "Basic" },
          { "value": "premium", "text": "Premium" }
        ]
      },
      {
        "type": "boolean",
        "name": "isAccountAdmin",
        "title": "Account administrator",
        "isRequired": true
      }
    ]
  },
  "presets": [
    {
      "name": "Basic member",
      "description": "A Basic customer without access to account billing.",
      "variables": {
        "customerTier": "basic",
        "isAccountAdmin": false
      }
    },
    {
      "name": "Premium member",
      "description": "A Premium customer with priority support, but no access to account billing.",
      "variables": {
        "customerTier": "premium",
        "isAccountAdmin": false
      }
    },
    {
      "name": "Premium administrator",
      "description": "A Premium customer with priority support and access to account billing.",
      "variables": {
        "customerTier": "premium",
        "isAccountAdmin": true
      }
    }
  ]
};
```

### `src/SurveyCreatorComponent.jsx`

```js
import React from "react";
import { SurveyCreator, SurveyCreatorComponent } from "survey-creator-react";
import "survey-core/survey.i18n";
import "survey-creator-core/survey-creator-core.i18n";
import { surveyJSON } from "./survey_json";
import { variablePresets } from "./variable_presets";

import "survey-core/survey-core.css";
import "survey-creator-core/survey-creator-core.css";
import "./index.css";

import SurveyTheme from "survey-core/themes";
import { registerCreatorTheme } from "survey-creator-core";

registerCreatorTheme(SurveyTheme); // Add predefined Survey Creator UI themes

function SurveyCreatorRenderComponent() {
    const creator = new SurveyCreator({ variablePresets });
    creator.JSON = surveyJSON;
    creator.activeTab = "preview";
    
    return (<SurveyCreatorComponent creator={creator} />);
}

export default SurveyCreatorRenderComponent;
```

### `src/index.css`

```css
/* You can add your custom CSS here. */
```

### `src/index.js`

```js
import React from "react";
import { createRoot } from "react-dom/client";
import SurveyCreatorRenderComponent from "./SurveyCreatorComponent";

const root = createRoot(document.getElementById("surveyCreatorContainer"));
root.render(<SurveyCreatorRenderComponent />);
```

### `package.json`

```json
{
  "dependencies": {
    "react": "latest",
    "react-dom": "latest",
    "survey-core": "latest",
    "survey-react-ui": "latest",
    "survey-creator-core": "latest",
    "survey-creator-react": "latest"
  },
  "devDependencies": {
    "react-scripts": "latest"
  }
}
```

## Other Frameworks

- [Angular](https://surveyjs.io/survey-creator/examples/test-runtime-dependent-survey-logic/angular.md)
- [Vue 3](https://surveyjs.io/survey-creator/examples/test-runtime-dependent-survey-logic/vue3js.md)
- [jQuery](https://surveyjs.io/survey-creator/examples/test-runtime-dependent-survey-logic/jquery.md)
- [Vanilla JS](https://surveyjs.io/survey-creator/examples/test-runtime-dependent-survey-logic/vanillajs.md)
