# Component Lifecycle

When component is first displayed on the page, i.e. the Xamarin Forms widget is added to the page visual tree ReactorUI calls the method OnMounted().&#x20;

Before component a component is removed from the page visual tree ReactorUI calls the OnWillUnmount() method.

OnMounted() is the ideal point to initialize the component for example calling web services or query local database to get the require information to render the component in the Render() method.

For example in this code we'll show an activity indicator while component is loading:

```csharp
public class BusyPageState : IState
{
    public bool IsBusy { get; set; }
}

public class BusyPageComponent : RxComponent<BusyPageState>
{
    protected override void OnMounted()
    {
        SetState(_ => _.IsBusy = true);

        //OnMounted is called on UI Thread, move the slow code to a background thread
        Task.Run(async () =>
        {
            //Simulate lenghty work
            await Task.Delay(3000);

            SetState(_ => _.IsBusy = false);
        });

        base.OnMounted();
    }

    public override VisualNode Render()
    {
        return new RxContentPage()
        {
            new RxActivityIndicator()
                .VCenter()
                .HCenter()
                .IsRunning(State.IsBusy)
        };
    }
}

```

and this is the resulting app:

![Slow loading sample page](https://3850152230-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3fGRH7kWyk6bJDMyZV%2F-M3z0rVCqAaK_K7CrihV%2F-M3z6leTOHvPeTwXSTx2%2FReactorUI_BusyDemo.gif?alt=media\&token=862f7407-013b-4fbf-bbeb-7de66313f555)

NOTE: Do not use constructors to pass parameters to the component but public properties instead (take a look at the Custom Components documentation page.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://adospace.gitbook.io/reactorui/guide/components-with-a-state/component-lifecycle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
