Component life-cycle
Describes the component life-cycle
Any MauiReactor application is composed of a logical tree of components. Each component renders its content using the overload function Render()
. Inside it, the component can create a logical tree of Maui controls and/or other components.
It's important to understand how components are Created, Removed, or "Migrated" when a new logical tree is created after the current tree is invalidated (calling a method like SetState
or Invalidate
).
The key events in the life of a component are:
Mounted (aka Created) is raised after the component is created and added to the logical tree. It's the ideal place to initialize the state of the component, directly setting state properties or calling an external web service that will update the state. You can think of this event more or less like a class constructor.
WillUnmount (aka Removing) is raised when the component is about to be removed from the logical tree. It's the perfect place to unsubscribe from service events or deallocate unmanaged resources (for example, use this overload to unsubscribe from a web socket event in a chat app).
PropsChanged (aka Migrated) is raised when the component has been migrated to the next logical tree. It's the ideal place to verify whether it is required to update the state. Often this overload contains code similar to the code used for the Mounted event.
Each of these events is called under the main UI dispatcher thread so be sure to put any expensive call (for example calls to the network or file system) in a separate task using the async/await pattern.
To better understand how these events are fired we can create a sample like the following tracing down in the console when they are called:
Running this code you should see an app like this:
Playing a bit with it, you should be able to see tracing lines like the following that could help to understand how events are sequenced:
Last updated