Using Reactor

Microsoft.UI.Reactor is a declarative, component-based UI framework for WinUI. You write your UI in C# as components that return a description of the UI from a Render() method, and keep their state in hooks such as UseState. Reactor turns that description into real WinUI controls and updates only what changed when the state changes.

Uno Platform runs the same Reactor code on desktop (Windows, macOS, Linux), WebAssembly, Android and iOS.

Note

Reactor support is in preview and requires Uno Platform 7.0 or later, with a .NET 10 SDK. A project that uses Reactor runs on the Uno Platform targets only; it can't also target WinAppSDK (see UNOB0031).

Create an app

You can write a Reactor app as a single .cs file, or start a full project from the unoapp-reactor template.

A Reactor app can be a single .cs file, with no project file.

Save this code as Counter.cs:

// Use a Uno.Sdk version 7.0 or later
#:sdk Uno.Sdk@x.y.z
#:property TargetFramework=net10.0-desktop
#:property OutputType=Exe
#:property UnoSingleProject=true
#:property UnoFeatures=Reactor
#:property PublishAot=false
#:property PackAsTool=false

using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using static Microsoft.UI.Reactor.Factories;

ReactorApp.Run<CounterApp>("Counter", width: 480, height: 360);

class CounterApp : Component
{
    public override Element Render()
    {
        var (count, setCount) = UseState(0);

        return VStack(12,
            Heading($"Count: {count}"),
            HStack(8,
                Button("-", () => setCount(count - 1)),
                Button("+", () => setCount(count + 1))
            )
        ).Padding(24);
    }
}
Tip

To keep the version out of the file, write #:sdk Uno.Sdk without a version, and name the Uno.Sdk version in a global.json next to the file:

{
  "msbuild-sdks": {
    "Uno.Sdk": "x.y.z"
  }
}

UnoFeatures=Reactor adds the Uno.Microsoft.UI.Reactor package, at the version that matches your Uno.Sdk.

Then run it:

dotnet run Counter.cs

A single file targets one platform at a time. To run it on WebAssembly or Android, change the TargetFramework line to net10.0-browserwasm or net10.0-android. A single file can't run on iOS yet: for iOS, create a project from the template instead.

Hot Reload

When you run the app from an IDE with the Uno Platform extension, Hot Reload applies your changes to a component's Render() method while the app is running, and the values held by UseState are kept. This works on desktop, WebAssembly, Android and iOS, and needs no Reactor-specific setup.

From the command line, run the desktop target with dotnet watch. A project from the template lists several target frameworks, so override the list with the one to run:

dotnet watch --property:TargetFrameworks=net10.0-desktop

Next steps

  • Build a complete app step by step with the Counter tutorial.
  • For the controls and features supported on each platform, see the Reactor on Uno Platform README.
  • To learn more about components, hooks and the element factories, see Microsoft's Reactor guide. Its code runs on Uno Platform unchanged, but skip its setup steps (winapp, MSIX packaging), which are for Windows: create your app as shown above instead.