# Set State

The Set State action updates the state of a selected state container, page, or component. It allows you to modify specific pieces of state data dynamically, optionally triggering a UI rebuild to reflect these changes immediately.

{% embed url="<https://www.youtube.com/watch?v=1BLTQ1ldWaA>" %}

Watch this on Youtube: <https://www.youtube.com/watch?v=1BLTQ1ldWaA>

## Properties

| Property        | Type      | Required | Description                                                                                                                                                    |
| --------------- | --------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `State`         | Dropdown  | Yes      | Select the state container, page, or component whose state you want to update.                                                                                 |
| `State Updates` | Array     | No       | List of variable updates within the selected state. Each update contains a State Name (dropdown) and Value pair. Use "Add New" button to add multiple updates. |
| `Rebuild UI`    | `boolean` | No       | Whether to rebuild the UI immediately after the state update. Defaults to `false`.                                                                             |

## Use Cases

* **Form Field Updates**: Modify form input values based on user interactions
* **UI State Management**: Control visibility, enabled state, or styling of components
* **Data Synchronization**: Update state with API response data
* **Interactive Controls**: Toggle switches, checkboxes, or selection states
* **Dynamic Content**: Change displayed content based on user selections

## Default Behavior

By default:

| Behavior                  | Description                                                                   |
| ------------------------- | ----------------------------------------------------------------------------- |
| **State Updates**         | All updates in the `State Updates` array are applied atomically               |
| **UI Rebuild**            | When `Rebuild UI` is `false`, state changes may not immediately update the UI |
| **Expression Evaluation** | All expressions in `Value` fields are evaluated in the current scope context  |
| **Error Handling**        | Invalid state context names throw an error                                    |

## How to Use

<figure><img src="https://3626461507-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbhApDTL7kHrXte2TTtjs%2Fuploads%2Fgit-blob-503984c14e90b9f3c3c6c311f4b7c2b154f82467%2Fset_state_action.png?alt=media" alt="Call External Method action configuration showing message name and payload fields"><figcaption><p>Call External Method action configuration in Digia Studio</p></figcaption></figure>

1. Attach the action to a widget event (button tap, form submission, etc.)
2. Select **Set State** from the action list
3. Select the `State` from the dropdown to choose the target state container
4. In the `State Updates` section, select a variable `State Name` from the dropdown for each update
5. Set the corresponding `Value` for each variable
6. Click **Add New** to add additional variable updates as needed
7. Optionally enable `Rebuild UI` for immediate visual updates

## Implementation Details

The action requires a `StateContextProvider` ancestor in the widget tree to locate the specified state context. Each update in the `State Updates` array specifies a `State Name` and `Value` pair.

When `Rebuild UI` is `true`, the state context notifies listeners to trigger a UI rebuild. When `false`, the state is updated but the UI may not immediately reflect changes until the next natural rebuild.

State values can be any type (strings, numbers, booleans, objects, arrays) and support expressions for dynamic evaluation.

## Related Actions

* [Set App State](https://docs.digia.tech/logic-and-interaction/actions/set-app-state) - Update global application state
* [Rebuild State](https://docs.digia.tech/logic-and-interaction/actions/rebuild-state) - Trigger UI rebuilds without changing state values
* [Execute Callback](https://docs.digia.tech/logic-and-interaction/actions/execute-callback) - Handle state changes with custom logic

***
