ReactorUI
  • What's ReactorUI for Xamarin
  • Guide
    • Setting up
    • Hello World Sample
    • Components
      • Component Lifecycle
      • Component Properties
      • Component with children
      • Navigation
      • Component Context
      • Reference to native controls
      • Animation
  • Native Tree and Visual Tree
    • Custom visual nodes
    • Custom Pages
  • Migrating from MVVM model
  • Integrating DI Containers
  • Hot reload troubleshooting
  • Styling and theming
  • Sample with SkiaSharp
Powered by GitBook
On this page

Was this helpful?

  1. Guide

Components

There are 2 kind of components: Stateless components that we saw in the Hello World sample and Stateful components that maintains a state.

To create a stateful component in ReactorUI just derive from RxComponent<T> where T is a class that defines the state of the component.

Let's change the Hello World project adding a component that implement a counter page. Replace the code in the MainComponent.cs file with the following (you can edit the file on the fly and hot reload the page):

public class MainComponentState : IState
{
    public int Counter { get; set; }
}

public class MainComponent : RxComponent<MainComponentState>
{
    public override VisualNode Render()
    {
        return new RxNavigationPage()
        {
            new RxContentPage()
            {
                new RxStackLayout()
                {
                    new RxLabel($"You clicked {State.Counter} times!")
                        .FontSize(Xamarin.Forms.NamedSize.Large),
                    new RxButton("Click Here!")
                        .OnClick(OnButtonClicked)
                }
                .VCenter()
                .HCenter()
            }
            .Title("Hello World")
        };
    }

    private void OnButtonClicked()
    {
        SetState(s => s.Counter++);
    }
}

PreviousHello World SampleNextComponent Lifecycle

Last updated 5 years ago

Was this helpful?