defineComponent
defineComponent() wraps a factory function into a component definition that can be registered on a Lume app.
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
factory | (ctx: ComponentContext) => T | A function that sets up the component and returns its public API |
Returns
Section titled “Returns”A ComponentDefinition object that can be passed to app.component().
The Factory Function
Section titled “The Factory Function”The factory function is called once per component instance (per data-lume element found during mount). It receives a Component Context object with all the tools needed to build reactive behavior.
What to Do in the Factory
Section titled “What to Do in the Factory”- Query parts — Use
part()andparts()to find named elements - Create signals — Use
signal()for reactive state - Register effects — Use
effect()for DOM updates - Bind events — Use
on()for event listeners - Return public API — Expose methods for external access
Return Value
Section titled “Return Value”The returned object becomes the component’s public API, accessible via app.get(id) or app.require(id).
If you don’t need a public API, you can return an empty object or omit the return:
Type Safety
Section titled “Type Safety”The generic parameter T is inferred from the return type of the factory: