Latest trend in Dynamics 365 world is Power Apps Component Framework or in short PCF. Its pretty cool to have PCF components in our environment which gives us great visualization for the same data and also better usability for the end users. Understanding how these components work or in general the end to end lifecycle of this framework is really important to make sure that we develop these components the right way and triggers the required logic at the right time. In this blog we will try to understand the lifecycle of a Power Apps component in general and main methods which gets called during this life cycle.
Before we get into the life cycle of the component, lets try to understand the different methods which we will come across when we start understanding the life cycle. Below are the four major methods which we need to understand about what it does and where we have to use each of them. These methods are invoked through framework Runtime process.
- init: This is a mandatory method to be implemented. This method initializes the component instance. Any kind of server calls and other actions related to the initialization can be called inside this method. We cannot initialize the Dataset values in this method.
- updateView: This is also a mandatory method which needs to be implemented. This method will get triggered when the value of any component’s property gets changed.
- getOutputs: PCF framework calls this method prior to receiving new data. In case we have any property which needs to be managed where the properties changes dynamically this is the method to be considered.
- destroy: This is a mandatory method to be implemented. This gets invoked when the component is to be removed from the DOM. This can be used to cleanup and release any memory the component might be using. Something similar to our destructor method in most of the programming languages.
- notifyOutputChanged: This method is mostly required for interactive components where we have to notify the host about output changed.
Now lets see how it works. The PCF framework first calls the init method of our components. In case, we are creating an interactive component, we have to notify the host that the component’s output has changed by calling the nofifyOutputChanged method. Then the framework will call getOutputs methods to get values for all bound properties of your component. Then the runtime will notify the host which will perform the validation on the output. If its valid, it will call the updateView method. If not valid, it will call the updateView method and pass the old value along with the error message. This is depicted via picture below (PC: Microsoft Documentation)
A picture talks more than all these stories, hope by now you are clear on the life cycle of a PCF component from the moment it gets loaded to the point where some data gets changed in the same control. This can be extended to get data from the same data source or from other fields in the application. Will try to come up with more blog towards those areas in the coming days. Thanks for your time reading this blog and hope it helped in understanding more towards the lifecycle of the PCF components. Stay tuned.