GitHub - theoriclabs/lean-react

GitHub

7 min read Original article ↗

LeanReact

Write React components in Lean. They compile to JavaScript and render with React. Props, state, hooks, and components you pass into other components stay the familiar building blocks.

v0.6.0 release · How-to guide · Frontend docs · MIT license

LeanReact playground with a live counter beside its Lean source

LeanReact is experimental. It uses Lean 4.36.0 and provides UI libraries and the LeanJS compiler, with portable domain and client abstractions from pinned dependencies. Server routing, authentication and persistence belong in LeanAPI. APIs can change.

The optional server adapter serves compiled pages alongside LeanAPI routes.

A counter in Lean

Start with a typed props record, a hook, and a DOM node. This is an ordinary React function component, written in Lean:

import LeanReact
open LeanReact

structure CounterProps where
  label : String

def Counter : Component CounterProps := component fun props => do
  let count ← useState 0 "count"
  pure <| DOM.button {
    onPress := some (count.modify (fun value => value + 1))
  } #[
    text (props.label ++ ": " ++ toString count.value)
  ]

Read it the way you would read React:

  • structure CounterProps is the props type, like a TypeScript interface.
  • useState gives you count.value, count.set, and count.modify. The last one is setCount(previous => previous + 1). "count" is a stable hook label.
  • DOM.button creates a button; #[...] is its children. onPress runs the update when clicked.

A few syntax hints: fun value => ... is an arrow function, some supplies an optional prop, and ++ joins strings. ← reads a hook; pure <| returns the rendered element.

The first-form tutorial walks through complete files, including compiling and mounting a component.

Compose by passing pieces

Pass an editor into a form. Pass a row component into a list. Share a custom hook between two layouts. These are ordinary values and functions, so you can change one piece without rewriting the screen.

Ticket workspace with a saved edit and replaceable layouts and editors

The playground includes:

  • A ticket workspace with interchangeable layouts and field editors.
  • Collection forms that keep each row’s state when you reorder them, and keep invalid input while you edit.
  • A context provider and consumer built in separate libraries that share live updates.

Collection form with keyed rows, validation errors, and the Lean source that composes the editor

Shared domain definitions describe application data and its rules. The same title validator can run in your form and on a Lean backend. Start with a type and a parsing function; add richer descriptions when the application needs them.

Bugs that don't compile

Some common React bugs are type errors or build failures here, not runtime surprises.

Forgetting a loading, empty, or error state. useResource returns one closed type: idle, loading, success, or failure. The data only exists inside success, so the only way to read it is a match, and the match must be exhaustive.

import LeanReact
open LeanReact

def loadTickets (_ : ResourceRequest) : Action (Except String (Array String)) :=
  pure (.ok #["Fix the login page"])

def TicketList : Component Unit := component fun _ => do
  let tickets ← useResource (Key.string "tickets") loadTickets #[] true "tickets"
  pure <| match tickets.state with
    | .idle => empty
    | .loading _ => DOM.p {} #[text "Loading…"]
    | .failure _ (.loader message) => DOM.p { role := some "alert" } #[text message]
    | .failure _ (.call .unauthenticated) => DOM.p { role := some "alert" } #[text "Sign in first."]
    | .failure _ (.call failure) => DOM.p { role := some "alert" } #[text ("Request failed: " ++ failure.code)]
    | .failure _ (.exception message) => DOM.p { role := some "alert" } #[text ("Unexpected: " ++ message)]
    | .success _ items => DOM.ul {} (items.map fun item => DOM.li {} #[text item])

Delete the .idle and .loading arms and the compiler answers Missing cases. A typed loader error, a transport failure (.call, a closed CallFailure type), and an unexpected exception are separate constructors, so they cannot be conflated by accident. "Loading and failed at once" has no constructor at all.

Setting state during render. Render runs in Hook; state updates are Action values. There is no way to run one inside the other, so this is a type error:

import LeanReact
open LeanReact

def Broken : Component Unit := component fun _ => do
  let count ← useState 0 "count"
  count.set 0   -- error: Action Unit, expected Hook Unit
  pure <| text (toString count.value)

Updates go in event handlers (onPress := some (count.set 0)) or in useEffect.

Conditional hooks. The compiler inspects every component and custom hook and refuses to emit JavaScript when a hook sits behind a branch, inside a loop, or in a recursive call. It follows your custom hooks with their real arguments, so the check is not based on a use prefix. To render state conditionally, render a child component conditionally; each element is its own hook boundary.

Null and typos. Optional fields are Option, so ticket.assignee has to be matched before it is used. Statuses are inductives, so "in-progress" versus "inProgress" is not a value you can write, and adding a status breaks every match until it is handled.

Duplicate keys. keyedEach takes a key function over the item, not an index, and throws on a duplicate sibling key in both the reference interpreter and the browser.

This is not a proof of correctness. Stale closures, wrong effect dependencies, CSS, and business logic that typechecks but does the wrong thing are still yours. The frontend guide lists what the tests exercise.

For a longer worked example, checkout state explosion takes a nine-field checkout form from 12,288 representable states down to a type with exactly 888, and has Lean count both.

CSS and existing React libraries

Use regular CSS files and className props. JavaScript and TypeScript can consume the generated components too.

Existing React libraries need an explicit binding between their props and Lean. There is a working foreign-component example in the interop guide. shadcn and Tailwind are not configured out of the box.

Run locally

You’ll need Git, Node 22.13 or newer, and elan, the Lean version manager. Think of elan like nvm: it installs the Lean version this project selects. Follow its installation instructions for your OS, then open a new terminal.

git clone https://github.com/theoriclabs/lean-react.git
cd lean-react
npm ci
npm run dev

Open http://localhost:4173. The first build may download the Lean compiler. The examples run in browser memory, so you can try them immediately.

Start by editing the counter component. For your own application, lake exe leanreact build|dev|test generates, bundles, serves and checks its pages. The dev server rebuilds when you save; refresh the browser to see the change.

Lean versions

LeanJS reads Lean's compiler IR, so each LeanReact release supports one Lean minor version. Pin LeanReact by the tag for your Lean version:

Lean LeanReact Notes
4.36 v0.6.0, v0.5.1, v0.5.0, v0.4.2, v0.4.1, v0.4.0 Current main
4.35 6b81e8b Development revision
4.33 v0.2.0-rc.1, v0.1
[[require]]
name = "leanreact"
git = "https://github.com/theoriclabs/lean-react"
rev = "v0.6.0"

A scheduled CI job builds and tests main against the newest Lean release candidate, so a break shows up before that Lean version is released. Releasing describes how a release is cut.

What to expect

This is an experimental library for Lean-authored frontends. The examples demonstrate working components, forms, asynchronous data loading, and shared domain rules. Only a subset of Lean compiles to JavaScript today; React Server Components and automatic JS/TS bindings are still ahead. See current support and limits.

To build your own screen, follow the how-to guide. If you’re using a coding agent, ask it to read SKILL.md first.

Application code lives in examples/; reusable library code lives in engine/. Contributor checks and architecture details are in the development guide.