Home › Concepts
Components
In Flux, components are plain Luau functions that run once to construct Roblox Instances and wire up reactive bindings.
type ButtonProps = {
text: Flux.Reactive<string>,
onClick: (() -> ())?,
}
local function PrimaryButton(props: ButtonProps): TextButton
return Flux.new "TextButton" {
Size = UDim2.fromOffset(200, 50),
BackgroundColor3 = Color3.fromRGB(0, 120, 255),
Text = props.text,
Activated = function()
if props.onClick then
props.onClick()
end
end,
}
endWhen state changes, PrimaryButton itself doesn't re-run. Only the bound Text property updates.
Consuming Components
Call component functions directly inside a Flux.new hierarchy:
local count = Flux.signal(0)
local screen = Flux.new "Frame" {
Size = UDim2.fromScale(1, 1),
PrimaryButton({
text = function()
return `Count: {count()}`
end,
onClick = function()
count(count() + 1)
end,
}),
}Handling Props (Static vs. Reactive)
Callers may pass a static value ("Submit"), a reactive Node, or a getter function (() -> string). Annotate flexible component props with Flux.Reactive<T> (which accepts T | Node<T> | () -> T).
1. Passing Props Directly to Instance Properties
Flux.new and Flux.edit accept static values, nodes, and getters natively, so you can pass a Flux.Reactive<T> prop straight to an Instance property:
local function StatusLabel(props: { status: Flux.Reactive<string> })
return Flux.new "TextLabel" {
Text = props.status, -- static strings assign once, nodes and getters bind reactively
}
end2. Reading Node-or-Value Props with Flux.read
When you need to transform a Flux.Reactive<T> prop inside a computation, call Flux.read(value). It reads a node or getter with tracking, or returns a plain value as-is:
local function StatusLabel(props: { status: Flux.Reactive<string> })
return Flux.new "TextLabel" {
Text = function()
return `Status: {string.upper(Flux.read(props.status))}`
end,
}
endUse Flux.isNode(value) when you explicitly need to check whether a value is a Flux node.
Splitting Props with Flux.split
Reusable components often accept both component-specific props (like label or accent) and pass-through Instance properties (like Size, Position, LayoutOrder, or children) in one table.
Declare your component's own prop schema once with Flux.prop(), derive the full prop table type with Flux.Props<ClassName, typeof(OWN)>, and separate them at runtime with Flux.split(props, OWN):
local OWN = {
label = Flux.prop() :: Flux.Reactive<string>,
accent = Flux.prop() :: Flux.Reactive<Color3>?,
padding = Flux.prop(8), -- optional for callers, always `number` inside the component
}
type BadgeProps = Flux.Props<"TextLabel", typeof(OWN)>
local function Badge(props: BadgeProps): TextLabel
local own, rest = Flux.split(props, OWN)
return Flux.edit(Flux.new "TextLabel" {
Text = function()
return `[ {Flux.read(own.label)} ]`
end,
TextColor3 = Flux.fallback(own.accent, Color3.fromRGB(120, 170, 255)),
Flux.padding(own.padding),
})(rest)
end- Single source of truth:
Flux.prop()marks the key inOWN, while the type cast beside it givestypeof(OWN)andown(Flux.Pick<BadgeProps, typeof(OWN)>) their exact--!strictproperty types (keeping required props required). Pass a default value (Flux.prop(default)) to make a key optional inFlux.Propswhile guaranteeing a non-nildefaultvalue insideownwhenever callers omit it or passnil(plain unfrozen table defaults without metatables are deep-cloned on eachFlux.splitcall so mutations don't leak across component instances). Annotated key-literal arrays likelocal OWN: { "label" | "accent" } = { "label", "accent" }also preserve exactPickandOmittypes on a single group (two or more groups widen toFlux.Partial<T>). - Pass-through forwarding:
restis typed asFlux.Omit<BadgeProps, typeof(OWN)>and holds every remaining property, event, numeric child, and__ATTR/__EVENT/__TAG/__CLEANdirective without mutatingprops. - Composing schemas with
Flux.own: Freeze a standalone schema or merge several shared schemas into one withFlux.own(...):
local BASE_OWN = Flux.own({
gap = Flux.prop(8),
label = Flux.prop() :: Flux.Reactive<string>,
})
local SEARCH_OWN = Flux.own(BASE_OWN, {
placeholder = Flux.prop() :: Flux.Reactive<string>?,
})
type SearchBoxProps = Flux.Props<"TextBox", typeof(SEARCH_OWN)>Merging Props with Flux.merge
Use Flux.merge(...) to build a read-only live view across 1 to 4 tables (left-to-right, where the rightmost non-nil value wins). It forwards component defaults alongside dynamic props without baking static copies.
local function Card(props: Flux.Props<"Frame">?)
local resolved = Flux.merge({
BackgroundColor3 = Color3.fromRGB(28, 30, 38),
Size = UDim2.fromOffset(320, 180),
}, props)
return Flux.new "Frame"(resolved)
endIndexing resolved forwards directly to the rightmost source table defining that key, so reactive getters, nodes, and events stay intact without cloning or eager evaluation. Because Flux.merge is shallow per key, rightmost numeric children and directive tables (__ATTR, __TAG, __CLEAN) overwrite earlier ones at the same key; wrap { base, rest } as nested arrays at numeric indices or chain Flux.edit(base)(rest) when you want defaults and caller additions to accumulate. In --!strict, a non-optional key on any merged table narrows away optionality from earlier tables (Flux.Merge<A, B>). Flux.new and Flux.edit accept Flux.merge(...) views directly, or you can call Flux.split(resolved) to materialize a plain writable copy when needed.
Accepting Children
Arrays placed at a numeric index inside Flux.new or Flux.edit flatten into children automatically while preserving sequential LayoutOrder:
local function Panel(props: { title: Flux.Reactive<string>, children: Flux.Children? })
return Flux.new "Frame" {
Size = UDim2.fromOffset(400, 300),
Flux.list({ gap = 8 }),
Flux.new "TextLabel" { Text = props.title },
props.children, -- flattened in place
}
endComponent Scopes and Lifecycles
When you call a component inside a parent Flux.scope, everything it creates attaches to that parent scope automatically.
When a standalone component creates internal Effects or resources whose lifetime should follow its own root Instance, wrap its body in Flux.scope and bind the scope to __CLEAN:
local function TimerLabel(): TextLabel
local _, label = Flux.scope(function(scope)
local elapsed = Flux.signal(0)
Flux.cleanup(Flux.Clock.connect("Heartbeat", function(dt)
elapsed(elapsed() + dt)
end))
return Flux.new "TextLabel" {
Text = function()
return string.format("%.1fs", elapsed())
end,
__CLEAN = { scope },
}
end)
return label
end--!strict Types
Flux exports its core reactive and component types directly on the Flux module:
| Type | Meaning |
|---|---|
Flux.State<T> | Writable Signal or SyncSignal node (:set is checked against T) |
Flux.Node<T> | Read/lifecycle reactive node (Signals, Computeds, Effects, Scopes, Springs, Tweens) |
Flux.Reactive<T> | T | Flux.Node<T> | () -> T (static value, node, or getter) |
Flux.Unwrap<T> | Extracts V from Node<V>, State<V>, Spring<V>, SyncSignal<V>, or () -> V (leaves plain T as-is) |
Flux.Props<ClassName, Extra?> | Flux.new(className) property table merged with Extra (your component's own props override conflicting class members) |
Flux.DefaultProp<T> | Schema default marker produced by Flux.prop(default) (optional in Props, non-nil T in split) |
Flux.Merge<Base, Extra> | Table merge where fields in Extra replace matching keys on Base |
Flux.Pick<T, K> / Flux.Omit<T, K> / Flux.Partial<T> | Table projection helpers used by Flux.split |
Flux.Component<P, I?> | (props: P) -> I (defaults I to Instance) |
Flux.Portal | Portal handle returned by Flux.portal |
Flux.Child / Flux.Children | Single child item, portal, or nested array accepted at numeric indices in Flux.new / Flux.edit |
Flux.Cleanable | Any value accepted by Flux.cleanup and Flux.dispose |
Flux.Owner | Active scope/context handle returned by Flux.getOwner() |
Flux.Namespace | Full type of the Flux module table (for dependency injection or plugin helpers) |