Power Apps Components: Variable Scope & Access App Scope Explained (Set vs Global)

My understanding of variables declared in custom component was unclear, so here is a memo of the results of my various experiments.

スポンサーリンク

Custom component and variables/collections

Variables and collections can be declared in custom components using the Set and Collect functions.

My understanding of variables declared within this custom component was unclear, so I experimented with a number of things.

*UpdateContext function cannot be used in custom components.

Preparation

As a preliminary step, create a component that simply stores the current time in a variable when a button is pressed and displays the value on a label.

I tried various settings of the access app scope to see what the scope of the variables declared within this component is.

Use Set function with access app scope turned off

First, check the operation with the access app scope “off”.
When a component is placed on the screen and a button in the component is pressed, a value is assigned to the variable.
If the application then tries to access this variable from the app, an error occurs as shown in the image.
Furthermore, if the same component is placed in multiple applications and one of the buttons is pressed,
only the label of the component for which the button was pressed will be displayed.

The results show that variables declared in components with access app scope “off” are only held within the component in which they are placed and cannot be referenced by other components or apps.
*Not shared with the app and furthermore not shared with other custom components.

Use Set function with access app scope turned on

Then turn on the access apres scope for this component.
Then, it becomes possible to reference variables from the app, which was not possible before,
and when multiple components are placed and a button is pressed, the time is displayed on all labels.
If you check the list of variables in the application, you will find that the variables were displayed in each component when the Access app scope is “off” and in the global variables when the Access app scope is “on”.

The results show that variables declared in components with access app scope “turned on” are treated as global variables of the app.
*Of course, if the same component is deployed more than once, other components will be affected.

Also check variables declared in collections

Create a component that declares a collection and conduct the same experiment.
If the button is pressed with access app scope “off”, only the component that pressed the button will be updated,
and if access app scope is “on”, the variable will be treated as global, so it will appear in all components.

Conclusion: Variable Scope Behavior Summary

Based on the experiments, here is how variables behave within Custom Components:

  • Access app scope “Off”:
    Variables are Locally Scoped. Each component instance maintains its own independent variable values. They cannot be accessed directly from the app or other components.
  • Access app scope “On”:
    Variables are Globally Scoped. They act like global variables in the app and share values across all component instances and the main app.

Key Takeaway

The most important finding is that turning Access app scope “Off” allows you to encapsulate data within each component instance. This behavior is crucial for building reusable, independent UI widgets that don’t conflict with the rest of your application.

Extra: If you want to refer to a variable from an app with access app scope “off”.

If you want to reference a variable from your app while the access application scope is “off,” prepare an output property in the component and set the variable in the component to that property,
and set the variable in the component to that property,
the value can be referenced from the app.

Related Articles

コメント

Copied title and URL