State-less Components

Describes what are State-less components and how to create them

Each MauiReactor page is composed of one or more Components which is described by a series of VisualNode and/or other Components organized in a tree.

The root component is the first created by the application in the Program.cs file with the call to the UseMauiReactorApp<TComponent>().

    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiReactorApp<MainPage>(app =>
            {
                app.AddResource("Resources/Styles/Colors.xaml");
                app.AddResource("Resources/Styles/Styles.xaml");

                app.SetWindowsSpecificAssectDirectory("Assets");
            })
#if DEBUG
            .EnableMauiReactorHotReload()
#endif

            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-SemiBold.ttf", "OpenSansSemiBold");
            });

        return builder.Build();
    }

The following code creates a component that renders a ContentPage with title "Home Page":

class MainPage : Component
{
    public override VisualNode Render()
     => ContentPage()
            .Title("Home Page");
}

Line 3. Every component must override the Render method and return the visual tree of the component

Line 5. The ContentPage visual node pairs with the ContentPage native control.

Line 6. The Title property sets the title of the page and updates the Title dependency property on the native page.

You can also pass the title to the Constructor:

class MainPage : Component
{
    public override VisualNode Render()
      => ContentPage("Home Page");
}

Line 5. The title of the page is set by passing it to the ContentPage constructor.

Running the app you should see an empty page titled "Home Page"

You can build any complex UI in the render method of the component but often it's better to compose more than one component to create a page or app.

For example, consider this component:

class MainPage : Component
{
    public override VisualNode Render()
        => ContentPage("Login",
            VStack(
                Label("User:"),
                Entry(),
                Label("Password:"),
                Entry(),
                HStack(
                    Button("Login"),
                    Button("Register")
                )
            )
            .Center()
        );
}

We could create a component like this:

partial class EntryWithLabel : Component
{
    [Prop]
    string _label;
    
    public override VisualNode Render()
      => VStack(
            Label(_label),
            Entry()
        );
}

and reuse it on the main page as shown in the following code:

class MainPage : Component
{
    public override VisualNode Render()
        => ContentPage("Login",
            VStack(
                new EntryWithLabel()
                    .Label("User:"),
                new EntryWithLabel()
                    .Label("Password:"),
                HStack(
                    Button("Login"),
                    Button("Register")
                )
            )
            .Center()
        );
}

Reusing components is a key feature in MauiReactor: decomposing a large page into small components that are easier to test is also beneficial to the overall performance of the application.

Last updated