Power Apps Variable Scope: What Happens When Declared in Custom Components?

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 aprescope is “off” and in the global variables when the access aprescope 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 apliscope “off”, only the component that pressed the button will be updated,
and if access apliscope is “on”, the variable will be treated as global, so it will appear in all components.

Conclusion.

In conclusion, variables declared within a component behave as follows

  • Access aprescope “off”: Keep a value for each placed component
  • Access app scope “on”: Keep the value as a global variable of the app

The revelation that it holds a value for each component that is placed is the takeaway of this project.
This use of variables within components is expected to expand the range of functions that can be realized using components.

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