Skip to content
Kite

Components

kitex.FC defines a component. Its render function takes one props value and returns a kitex.Node describing the UI. Calling the resulting component creates a node; Kitex calls the render function when it needs the subtree. Declare the component once, usually in a package variable. Re-registering it inside another render repeats setup such as inspecting the props type for a key.

type RowProps struct {
Key string
Title string
}
var row = kitex.FC("TaskRow", func(p RowProps) kitex.Node {
return kitex.Box(kitex.BoxProps{}, kitex.Text(p.Title))
})
var heading = kitex.FC("Heading", func(kitex.NoProps) kitex.Node {
return kitex.Text("Tasks")
})

Call heading() for a component with no props (kitex.NoProps is struct{}). Call row.Props(RowProps{Key: "t1", Title: "Review patch"}) to pass props. row() is also valid: it passes the zero value of RowProps, not the last props used.

Use kitex.FCC when the caller supplies part of the UI. Children are variadic nodes, separate from props. A card can decide where to place them without knowing which components they are:

type CardProps struct {
Title string
}
var Card = kitex.FCC("Card", func(p CardProps, children ...kitex.Node) kitex.Node {
heading := kitex.Box.Style(
demo.Default.SectionLabel.Margin(0),
kitex.Text(p.Title),
)
return kitex.Box.Style(
demo.Default.Column(),
heading,
kitex.Fragment(children...),
)
})
var CardDemo = kitex.FC("CardDemo", func(kitex.NoProps) kitex.Node {
items, setItems := kitex.UseState([]string{"Build", "Test", "Ship"})
rotate := func() {
current := items()
last := len(current) - 1
setItems(append([]string{current[last]}, current[:last]...))
}
return Card.Props(
CardProps{Title: "Tasks"},
kitex.Map(items(), func(item string, _ int) kitex.Node {
return kitex.Box(kitex.BoxProps{Key: item}, kitex.Text(item))
}),
ActionButton("Reorder", rotate),
)
})

card(child1, child2) uses zero-value card props; card.Props(p, child1, child2) supplies a title. Fragment groups siblings without inserting a DOM element. Use a Box instead when the group needs its own layout or event listeners.

kitex.Map returns a fragment, not a slice: pass its result as one child, without .... Put the key on the outer node returned by the mapper. For a component, Kitex reads the Key string field from its props.

type Task struct{ ID, Title string }
func taskList(tasks []Task) kitex.Node {
return kitex.Fragment(
kitex.If(len(tasks) == 0, func() kitex.Node {
return kitex.Text("No tasks")
}),
kitex.Map(tasks, func(t Task, _ int) kitex.Node {
return row.Props(RowProps{Key: t.ID, Title: t.Title})
}),
)
}

Use a stable ID unique among siblings. If a new task is inserted first, every later index changes, but each ID still identifies the same row. Local state (for example, whether a row is expanded) follows the task, not its old screen position.

An ordinary Go if works too. Map skips nil results and element children can be nil. If(false, fn) instead returns Empty(): it draws nothing but preserves a virtual sibling position. IfElse takes two already-evaluated nodes; IfElseFn calls only the selected branch. Keep hooks out of branch and mapper callbacks; put per-row hooks inside the row component.

On a parent update, Kitex can reuse a component’s previous subtree if its props compare equal. This is an optimization for larger subtrees; small ones simply render again. Safely comparable props, such as a struct of strings and ints, use Go equality. Other props use a depth-limited reflection comparison. If equality cannot be established, Kitex renders. For function props, UseCallback keeps a closure’s identity stable until its dependencies change.

Props equality does not suppress a component’s own UseState or UseReducer updates, or changes from a subscribed UseContext. For FCC, non-empty passed children on either the previous or current render also bypass the shortcut: the parent may have changed the children without changing the props.

For complex props on a frequently updated path, implement kitex.EquatableProps. Compare everything that affects output or behavior. Here the only field is an ordered list of labels, so the standard library’s slices.Equal suffices:

type LabelsProps struct{ Labels []string }
var _ kitex.EquatableProps = LabelsProps{}
func (p LabelsProps) Equal(other any) bool {
q, ok := other.(LabelsProps)
return ok && slices.Equal(p.Labels, q.Labels)
}

Use state for a value that should change what the user sees. Use UseRef for a handle needed later by an effect or event handler. Current persists across renders; assigning it doesn’t schedule one. This component attaches a kitex.Ref[*element.InputElement] to the input, then focuses the mounted element when the button is pressed:

var RefFocusDemo = kitex.FC("RefFocusDemo", func(kitex.NoProps) kitex.Node {
inputRef := kitex.UseRef[*element.InputElement](nil)
return kitex.Box.Style(
demo.Default.Column(),
kitex.Input(kitex.InputProps{
Ref: inputRef,
Placeholder: "Search tasks",
Style: demo.Default.Input,
}),
ActionButton("Focus input", func() {
if inputRef.Current != nil {
inputRef.Current.Focus()
}
}),
)
})

Use kitex.CreateRef[*element.InputElement]() outside a render when the caller owns the handle. Keep that ref and pass it in; creating a new one on every render defeats its purpose.

Wrap a subtree that should fail independently. Here documentID identifies the selected document, child is its preview component node, and the caller supplies the error reporter:

var Preview = kitex.FC("Preview", func(kitex.NoProps) kitex.Node {
failed, setFailed := kitex.UseState(false)
if failed() {
panic("preview failed")
}
return kitex.Button(kitex.ButtonProps{
Style: demo.Default.PrimaryButton,
OnPress: func() { setFailed(true) },
}, kitex.Text("Crash preview"))
})
func PreviewPane(
documentID string,
child kitex.Node,
report func(any, []byte),
) kitex.Node {
return kitex.Box.Style(
demo.Default.Column(),
kitex.ErrorBoundary(kitex.ErrorBoundaryProps{
Key: "preview",
ResetKeys: []any{documentID},
OnError: report,
Fallback: func(err any, reset func()) kitex.Node {
return kitex.Button(kitex.ButtonProps{
Style: demo.Default.PrimaryButton,
OnPress: reset,
}, kitex.Text("Retry preview"))
},
}, child),
)
}
  • Key identifies the boundary among siblings. If mounting, reconciling, or re-rendering a descendant panics (including a state-driven render), the failed subtree is unmounted. OnError, if set, receives the original panic value and Go stack. Fallback takes its place in the same DOM slot; without it, nothing is shown there.
  • reset() queues a fresh mount on the rendering thread. It can be called from a handler or a background goroutine. A change in ResetKeys also retries a failed boundary: entries are compared in order by deep equality. A retry starts with fresh child state and effects.
  • A panic in the fallback goes to the next enclosing boundary, not this one. Boundaries don’t catch panics in event handlers, layout or post-commit effects, or async work. Go evaluates arguments before calling ErrorBoundary: put fallible render logic inside the child component, not in an expression used to construct its props.

To start the taskCard component above with the terminal backend, pass a context whose cancellation ends the event loop:

func run(ctx context.Context) error {
b, err := uv.New()
if err != nil {
return err
}
eng := kite.New(b)
root := kitex.CreateRoot(eng)
root.Render(taskCard())
return eng.Run(ctx)
}

CreateRoot mounts a full-viewport container and registers cleanup on engine stop. root.Container() returns the underlying dom.Element. Call root.Render(next) to update the tree; root.Render(nil) clears it but leaves the root usable. root.Unmount() synchronously cleans up the tree and permanently closes the root. It doesn’t detach the container; subsequent renders and repeated unmounts do nothing.

For a pane in an existing UI, root := kitex.CreateRootIn(container) wraps a dom.Element without mounting it or registering engine-stop cleanup. Unmount that root when the pane closes. If you manage the lifecycle by container rather than by root handle, use the low-level pair kitex.Render(node, container) and kitex.Unmount(container).