---
title: "Build a reactive HTML component with C# state"
url: https://recursiveai.org/oneui/cookbook/create-html-component-with-csharp-state/
description: "Build a reactive HTML component with C# state in OneUI. Compile a counter template, click its button in memory, and see the rendered text update."
version: "1.0.0"
---

# Build a reactive HTML component with C# state

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in desktop windows or in a browser via WebAssembly with OneApp. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/create-html-component-with-csharp-state/create-html-component-with-csharp-state.zip)

Bind a button’s label to its C# click count.

```shell
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```html
<button on:click="Increment">
  Count: {Count}
</button>
```

Save the markup above as `Counter.html` in the project folder next to `Counter.cs`. No separate `.csproj` entry is needed for the template; the NuGet package finds it automatically.

[The example](https://recursiveai.org/oneui/cookbook/create-html-component-with-csharp-state/create-html-component-with-csharp-state.zip) is the full program below: unzip it and `dotnet run`.

The button starts at `Count: 0` and shows `Count: 1` after one click.

## The full program

Create a `dotnet new console` project and add the package as shown above, or use the download with all the files in place. The NuGet package includes the generator: it matches `Counter.html` to `Counter.cs` and implements the partial properties. The example’s project file pins the package with:

```xml
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />
```

`Counter.cs`:

```csharp
using OneUI.Components;

public partial class Counter
{
    [State]
    public partial int Count { get; set; }

    private void Increment() => Count++;
}
```

The generated `[State]` setter tells OneUI which bindings to update when `Count` changes.

Replace `Program.cs` with the code below. It mounts the component on a `ReferenceDocument`, an in-memory HTML tree, and prints the result without opening a window.

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var dom = new ReferenceDocument();
var ui = new UiContext(dom);
dom.Context = ui;
ui.Mount(new Counter());
Console.WriteLine(dom.ToHtml());
dom.Click("button");
Console.WriteLine(dom.ToHtml());
```

`dotnet run` prints:

```text
<button>Count: 0</button>
<button>Count: 1</button>
```

If `Increment` is misspelled in the template, OneUI reports `OUI0021` at build time. See [markup diagnostics](https://recursiveai.org/oneui/docs/diagnostics/).

## Variations

**Count in steps of five.** Replace the handler in `Counter.cs`:

```csharp
private void Increment() => Count += 5;
```

After one click the label reads `Count: 5`.

## Related recipes

- [Pass parameters to a child HTML component in C#](https://recursiveai.org/oneui/cookbook/pass-parameters-to-child-component/)
- [Two-way bind an HTML input to C# state](https://recursiveai.org/oneui/cookbook/two-way-bind-input-value/)

## The short answer

Use `on:click="Increment"` to call a C# method and `{Count}` to read the current count into the button’s text. Declare `Count` as a `[State]` partial property.

## What is component state in a C# user interface?

Component state is data that belongs to a component and can change what it displays. `Count` is an integer on the `Counter` instance, not text stored in the button. The click handler increments the integer. The text binding formats the latest value, so there’s no second copy of the count to keep in sync.

Use `[State]` for values that the component owns, such as the count or whether its details are expanded. Use `[Param]` when a parent supplies the value. If a value belongs to several components, keep it in their parent and pass it down; otherwise you can end up with separate counters that look like they represent the same thing.

## How do you separate HTML markup from C# logic?

Keep the elements and bindings in `Counter.html` and the properties and handlers in the matching top-level, non-generic `partial class Counter`. The build matches the template to the class and generates the implementation of the partial property and the code that creates and updates the button. The `on:click` attribute is a OneUI directive, not an HTML inline event handler: OneUI registers a listener that calls your C# method. A handler can take no argument, as it does here, or take a `MouseEvent` when it needs event data.

## What changes in the HTML after a click?

The reference document receives changes to its in-memory tree. In this example the button stays in place, and OneUI changes its text node. `ToHtml()` gives you a snapshot of the current tree, not a live HTML file. Opening a saved snapshot in a browser won’t run the C# click handler. To put the compiled component in a native window or a browser app, use a [OneApp host](https://recursiveai.org/oneapp/); the console program lets you check the binding without either one.
