Components and custom properties

Moving data, with custom properties, between your app and components in Power Apps can be difficult. In this post I will look at updating data inside a component.

An introduction to Custom Properties

Compared to groups or Containers in Power Apps components will give you custom properties. Like with groups and containers, components will give you the grouping of multiple controls. Custom properties however will give you a way to pass data into your component and to collect data out of your component.

Create custom properties in Power Apps
Create custom properties in Power Apps

The custom properties can have all sorts of different data types.

Moving data from the app to the component

When you have created your component in Power Apps and you added a custom property then you can simply view the value of this custom property, or when you have a property of the type Table you will see that it is set to a table.

Components and Custom Properties in Power Apps Microsoft Power Apps image 13
View the MyTable Property

If you want to set the Table you can use the expression editor. In my example below I’m setting the MyTable property to AppTable. AppTable is a Table that I created during the App’ OnStart using the following Set code

Set(AppTable, [1,2,3,4])
Components and Custom Properties in Power Apps Microsoft Power Apps image 14
Set the MyTable Property to the AppTable

Ok, that was the easy bit!

In one of my test apps, I’ve created a gallery to display the Table in a Gallery and a button Update App Table that will update the values of the AppTable.

Components and Custom Properties in Power Apps Microsoft Power Apps image 15

Then within the Component (The section in green) I’m displaying the same Table in a Gallery that is part of my component.

In my button’s OnSelect code I can now use the following to update my variable and the Galleries in my app and the one in my component will both be updated with new values

Set(AppTable, ForAll(AppTable.Value, Value + 1))
Components and Custom Properties in Power Apps Microsoft Power Apps image 17

This is great as the data inside my app can be used by the component. If I do a similar thing with the button inside my component however I found that the data isn’t updated outside the component. For the right reasons of course as my custom property is an input property/

Moving data from the component to the app

To move data from a component back to the app, you can create a custom properties similar to shown above, however this time you will need to select Output as the Property Type.

Components and Custom Properties in Power Apps Microsoft Power Apps image 16
Property Type Input or Output

I’m now creating a custom property MyTableOut

Components and Custom Properties in Power Apps Microsoft Power Apps image 18

To update all numbers in my gallery I’m now setting a variable varMyTable and this variable is used in the Components MyTableOut property that I created.

Set(varMyTable, ForAll(Component1.MyTable.Value, Value + 1))
Components and Custom Properties in Power Apps Microsoft Power Apps image 19

This now means that I can use the data send from the component to a 3rd gallery (orange in the below screen shot).

Components and Custom Properties in Power Apps Microsoft Power Apps image 20

This 3rd gallery uses the MyTableOut Custom property from my component.

Components and Custom Properties in Power Apps Microsoft Power Apps image 21

Ok, this is nice, but not great! We can now get to the data inside the component from within the app.

Update data outside the component

As we have seen so far we can take a table of data and push this into a component. We can then take the data out of the component’s custom output property and display this somewhere in Power Apps.

One problem here!

We want to use the data that we pushed into the component back into the same gallery where we initially used it. We can do that with the above code.

Set(AppTable, Component1_1.MyTableOut)

But when do we do this?

We need to run the code when there is an update ready and only when the update of the data is ready.

This is why in my example I’m using a table. You cannot do an expression in PowerApps where you compare two tables with something like:

If(Table1 = Table2, ...)

This is where I’m introducing another output property in my component of the Type Boolean. I’m calling this property Dirty.

Components and Custom Properties in Power Apps Microsoft Power Apps image 22

Now when I update the table inside my component I’m also going to set th Dirty property.

Components and Custom Properties in Power Apps Microsoft Power Apps image 23

So every time my table is updated by varDirty will be set to True. Then I set the Dirty value to varDirty.

Components and Custom Properties in Power Apps Microsoft Power Apps image 24

And the OnReset of my component is set to make sure that my varDirty variable defaults to false.

Components and Custom Properties in Power Apps Microsoft Power Apps image 25

Now I’m adding a timer to my screen and set the Start to Component1_1.Dirty

Components and Custom Properties in Power Apps Microsoft Power Apps image 26

And Finally I’m setting the OnStart code to

If(Component1_1.Dirty, Set(AppTable, Component1_1.MyTableOut)); Reset(Component1_1)
Components and Custom Properties in Power Apps Microsoft Power Apps image 27

The reset in the above code ensures that the component is reset and the onreset code is run to set the Dirty Property back to false and the timer code isn’t run until my table data is updated within the component again.

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as a Principal Architect at HybrIT Services Ltd. You can contact me using contact@sharepains.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue Reading

%d bloggers like this: