Skip to main content

Components - Server

Creating a New Component​

A component template object on the server side consists of two main parts: a unique identifier (tag) and a layout root (layoutRoot). To simplify the creation of new components, the Duit packages provide the ComponentDescription function.

info

Within a component, it is not advisable to explicitly set the controlled = true property or assign widget IDs. Such widgets will be treated as uncontrolled, and setting fixed widget IDs could lead to controller creation errors.

const ExampleComponent = ComponentDescription("ExampleComponent",
Text(
{
attributes: {
data: "Hellow, Duit!"
}
}
)
);

Data Binding​

Data binding in a widget layout involves assigning attribute values to composite components that will be received from the server. This is managed by the refs property of the attribute object, which holds a collection of ValueReference objects.

Each ValueReference object has three main components:

  • attributeKey — the name of the attribute property (e.g., data), where the value will be placed.
  • objectKey — the key of the property inside the data-containing object.
  • defaultValue — an optional parameter used if no matching value is found.
const ExampleComponent = ComponentDescription("ExampleComponent",
Text(
{
attributes: {
data: "Hellow, Duit!",
refs: [
{
attributeKey: "data",
objectKey: "text",
defaultValue: "Hellow, world!",
},
// Ref("data", "text"), //shortand
]
}
}
)
);

Using Components in Layouts​

Adding components to your layout code involves using the Component widget, which takes the target component tag and a data object for filling it as parameters.

Component({
tag: ExampleComponent.tag,
data: {
text: "Hellow, Duit!",
},
id: "some_id_value",
})

Updating Component State​

Widgets within a component are always uncontrolled, meaning they can't be updated directly via a controller. To update a single component, use the standard approach with an UpdateEvent event type.

Components are controlled widgets by default, so to update their state, you need to explicitly specify a unique ID and send back to the client a new event that describes the change using a data object.