Power Apps Next Level
Power Apps Next Level
POWER APPS
CANVAS
NEXT LEVEL
Modern State Management with UDFs,
UDTs, and Advanced Components
This guide aims to demonstrate how User Defined Functions (UDFs) and
User Defined Types (UDTs) can serve as powerful tools to create
scalable, maintainable, and robust applications.
Just so you know, the features we will cover in this document are
currently in preview or experimental stages, and this document is
intended for exploratory purposes only.
These patterns reveal code duplication and key design flaws, including
poor reuse of components and context variables, unclear separation
between logic and UI, overuse of UI controls for logic, and limited use of
custom functions to simplify repetitive tasks.
Therefore, let’s begin by exploring the strategies and practices that help
us prevent waste and improve our applications!
Named Formulas
Named formulas are an important first step toward a more structured
and modular approach in Power Apps Canvas development.
Source : https://www.microsoft.com/en-us/power-platform/blog/power-apps/power-fx-introducing-named-formulas/
While this approach fosters cleaner and more modular code, it still has
notable limitations. Ideally, we should be able to define true functions
that accept parameters, include type definitions, and support structured
logic.
Such capabilities would enable the creation of more advanced and
maintainable architectures, better aligned with software development.
return type
function body
In the following sections of this guide, we will examine the reasons why I
believe that adopting the 'fx' prefix for UDFs may be less than ideal.
From the previous examples, we’ve seen the use of standard types. But
what if we need greater flexibility and want to define custom types?
Source : https://www.microsoft.com/en-us/power-platform/blog/power-apps/power-fx-introducing-named-formulas/
In this case is defined a new type called PaperType is defined with three
properties: Name, Width, and Height, each explicitly specifying its type.
The custom data type feature, although still experimental, opens the
door to a broad range of solution design possibilities.
I have prepared a simple Power Automate flow that uses the HTTP action
to consume a public REST API provided by fakestoreapi.com. The HTTP
action calls the endpoint /products/{id} to retrieve information about a
specific product, then returns the response as text to Power Apps.
Then, in Power Apps under the formulas, I define the data model
corresponding to the response received from the REST API.
Using the button below, called 'Flow Button', we retrieve the response
data from the cloud flow. Inside its OnSelect property, I save the
response and then use ParseJSON, passing the appropriate type as an
argument.
This not only enhances development efficiency but also ensures a more
structured and scalable app architecture, reducing redundancy and
maintenance efforts.
We can think of components as the building blocks for constructing a
page.
Important: This guide does not cover the details of creating events from
components or enhanced component properties in general. I have already
prepared a dedicated guide that explains their behavior. If you haven’t seen it
yet, I recommend checking it out here. In the following pages, it will be
assumed that the concepts and benefits are already understood.
We are therefore centralizing all the logic related to the call at the
main app level, removing dependencies on a potential datasource,
such as a custom connector, which may be subject to changes.
State Management
It’s time to encapsulate everything we’ve covered so far and explore ways
to improve our Power Apps Canvas applications
Application state management enables efficient control and
synchronization of data, user interface, and behavior across different
components. This approach improves app scalability, reduces
development complexity, and prevents potential state inconsistencies,
ensuring a smoother and more reliable user experience.
Let’s try to figure out its behavior using this diagram
This approach not only prevents the duplication of collections but also
improves maintainability and ensures data remains centralized and
consistent throughout the app.
Component
Main App
The component includes an input property of type table to receive the list
of products to be rendered, as well as an event property that we will
explore shortly. Additionally, under formulas, I defined the data models
representing both the product item and the cart item.
Now we need to define our service to allows you to centralize cart state,
avoid duplication, and easily reuse logic across multiple components
without introducing the added complexity of a full reducer.
Within the useCart domain, I defined the core functions responsible for
managing the cart state: addToCart, removeFromCart, clearCart,
increaseQuantity, and decreaseQuantity.
Note that instead of using the 'fx' prefix, I use the service name followed
by the action. This makes it easy to immediately recognize the function
and distinguish it from those of other services.
The functions removeFromCart and clearCart are responsible for
managing item removal from the CartStore: removeFromCart deletes a
specific item, while clearCart resets the entire store
The approach outlined in this guide has proven to offer tangible benefits
in terms of maintainability and consistency within the application. By
centralising logic within dedicated services such as useCart, we avoid
fragmented or unsustainable patterns, like relying on hidden buttons,
toggles, or duplicated expressions scattered across multiple screens.
Thanks.
nicoloferranti.net