---
title: Dynamically Set and Reset Question Values
product: Form Library
description: Automatically populate or reset form fields based on other answers using conditional expressions. View our free JavaScript demo.
framework: React
source: https://surveyjs.io/form-library/examples/set-question-value-dynamically/reactjs
index: https://surveyjs.io/form-library/examples/overview.md
---

# Dynamically Set and Reset Question Values (React)

Survey questions support API properties that let you automatically set or reset a question's value based on specific criteria. In this demo, these properties are used to copy the shipping address when a user selects "Yes" for the "Shipping address same as billing" question.

## Set Question Values Dynamically

To set a question's value based on other answers, assign an [expression](/form-library/documentation/design-survey/conditional-logic#expressions) to the [`setValueExpression`](/form-library/documentation/api-reference/question#setValueExpression) property. The result of the expression will be used as the question's value. If you want this expression to be evaluated conditionally, use the [`setValueIf`](/form-library/documentation/api-reference/question#setValueIf) property to specify the condition. This property accepts a Boolean expression, and when it evaluates to `true`, the survey runs the `setValueExpression` and assigns the result to the question's value.

## Reset Question Values Conditionally

To reset a question's value when a certain condition is met, specify the [`resetValueIf`](/form-library/documentation/api-reference/question#resetValueIf) property with a Boolean expression. When this expression evaluates to `true`, the question's value will be reset to the [`defaultValue`](/form-library/documentation/api-reference/question#defaultValue).

Refer to the survey JSON schema (`json.js` file) for code examples.

## 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": "comment",
      "name": "billing-address",
      "title": "Billing address",
      "rows": 2
    },
    {
      "type": "boolean",
      "name": "shipping-same-as-billing",
      "title": "Shipping address same as billing",
      "defaultValue": "true"
    },
    {
      "type": "comment",
      "name": "shipping-address",
      "title": "Shipping address",
      "enableIf": "{shipping-same-as-billing} = false",
      "resetValueIf": "{shipping-same-as-billing} = false",
      "setValueIf": "{shipping-same-as-billing} = true",
      "setValueExpression": "{billing-address}",
      "rows": 2
    }
  ],
  "textUpdateMode": "onTyping"
};
```

### `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/set-question-value-dynamically/angular.md)
- [Vue 3](https://surveyjs.io/form-library/examples/set-question-value-dynamically/vue3js.md)
- [jQuery](https://surveyjs.io/form-library/examples/set-question-value-dynamically/jquery.md)
- [Vanilla JS](https://surveyjs.io/form-library/examples/set-question-value-dynamically/vanillajs.md)
