---
title: Expressions in Dynamic Panel
product: Form Library
description: Dynamic panels let you add a duplicate panel in order to continue a list of answers. View this free example for JavaScript to learn how to use expressions in such panels and calculate entered values on the fly.
framework: React
source: https://surveyjs.io/form-library/examples/how-to-use-expressions-in-dynamic-panel/reactjs
index: https://surveyjs.io/form-library/examples/overview.md
---

# Expressions in Dynamic Panel (React)

Expressions let you configure conditional logic to create dynamic surveys. You can use expressions to implement branching and skip logic, dynamically show and hide questions, mark them as required, enable and disable them, and perform calculations right in the survey JSON schema. This example demonstrates expressions in a [Dynamic Panel](/form-library/documentation/api-reference/dynamic-panel-model). Here expressions are used to calculate employment duration. Enter a company name and employment dates to see the number of days employed for this company. You can add as many companies as you want&mdash;days employed will be added up and displayed under the Dynamic Panel.

## Access Questions Within a Panel

To reference questions within the same panel in your expressions, use the `panel` prefix as follows: `{panel.questionName}`. Refer to the expressions within the Employment History panel for code examples.

## Dynamically Calculate Values

Expressions support built-in functions and allow you to create custom functions for in-survey calculations. In this demo, expressions calculate the number of days employed using built-in [`diffDays`](https://surveyjs.io/form-library/documentation/design-survey/conditional-logic#diffdays) and [`sumInArray`](https://surveyjs.io/form-library/documentation/design-survey/conditional-logic#suminarray) functions. For more information on how to use these and other built-in functions or how to implement a custom function, refer to the [Expressions](https://surveyjs.io/form-library/documentation/design-survey/conditional-logic#expressions) help topic.

## Manage Question Visibility

You can use expressions to implement conditional visibility for questions, choice options, matrix columns/rows, and other elements. In this demo, the number of days for individual companies is only displayed when you enter both the company name and employment dates. To implement this functionality, assign a Boolean expression to the [`visibleIf`](https://surveyjs.io/form-library/documentation/api-reference/question#visibleIf) property. You can find more details in the [Conditional Visibility](https://surveyjs.io/form-library/documentation/design-survey/conditional-logic#conditional-visibility) help topic.

## Validate Question Values

Expressions can be used to add data validation to your survey. In this example, the Employment Dates question fields use the `minValueExpression` and `maxValueExpression` properties to ensure that the end date falls within the range between the selected start date and the current date. For more information about data validation in SurveyJS Form Library, refer to the following help topic: [Data Validation](https://surveyjs.io/form-library/documentation/data-validation).

## Files

### `public/index.html`

```html
<div id="surveyElement" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; min-height: 100%; height:100%"></div>
```

### `src/SurveyComponent.jsx`

```js
import React from "react";
import { Model } from "survey-core";
import { Survey } from "survey-react-ui";
import "survey-core/survey-core.min.css";
import "./index.css";
import { json } from "./json";

function SurveyComponent() {
    const survey = new Model(json);
    survey.onComplete.add((sender, options) => {
        console.log(JSON.stringify(sender.data, null, 3));
    });
    return (<Survey model={survey} />);
}

export default SurveyComponent;
```

### `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 SurveyComponent from "./SurveyComponent";

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

### `src/json.js`

```js
export const json = {
  "elements": [
    {
      "type": "paneldynamic",
      "name": "employment-history",
      "title": "Employment History",
      "templateElements": [
        {
          "type": "text",
          "name": "company",
          "title": "Company",
          "isRequired": true
        },
        {
          "type": "multipletext",
          "name": "employment-dates",
          "title": "Employment dates",
          "items": [
            {
              "name": "start",
              "isRequired": true,
              "inputType": "date",
              "title": "Start",
              "maxValueExpression": "today()"
            },
            {
              "name": "end",
              "isRequired": true,
              "inputType": "date",
              "title": "End",
              "minValueExpression": "{panel.employment-dates.start}",
              "maxValueExpression": "today()"
            }
          ]
        },
        {
          "type": "expression",
          "name": "days-employed",
          "title": "Days employed by {panel.company}:",
          "visibleIf": "{panel.company} notempty and {panel.employment-dates.start} notempty and {panel.employment-dates.end} notempty",
          "titleLocation": "left",
          "expression": "diffDays({panel.employment-dates.start}, {panel.employment-dates.end})"
        }
      ],
      "panelCount": 1,
      "addPanelText": "Add company"
    },
    {
      "type": "expression",
      "name": "total-days-employed",
      "title": "Total days employed:",
      "titleLocation": "left",
      "expression": "sumInArray({employment-history}, 'days-employed')"
    }
  ],
  "checkErrorsMode": "onValueChanged"
};
```

### `src/theme.js`

```js
export const themeJson = {};
```

### `package.json`

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

## Other Frameworks

- [Angular](https://surveyjs.io/form-library/examples/how-to-use-expressions-in-dynamic-panel/angular.md)
- [Vue 3](https://surveyjs.io/form-library/examples/how-to-use-expressions-in-dynamic-panel/vue3js.md)
- [jQuery](https://surveyjs.io/form-library/examples/how-to-use-expressions-in-dynamic-panel/jquery.md)
- [Vanilla JS](https://surveyjs.io/form-library/examples/how-to-use-expressions-in-dynamic-panel/vanillajs.md)
