Skip to main content

Fragments

A Fragment is a special type of widget designed to implement flexible caching and handle parts of the UI that can be created in advance and do not require data at runtime. Using this type of widget allows for effective decomposition of large screens and optimization of application performance.

What Fragment Is For​

Fragment provides a mechanism for pre-creating and caching reusable UI elements. The main advantages are:

  • Performance: Pre-built fragments do not require repeated processing
  • Decomposition: Simplifies the structure of large screens by breaking them down into reusable parts
  • Caching: Static parts of the UI are created once and used multiple times
  • Optimization: Reduces runtime load by shifting processing to the build stage

How to Use Fragment​

Fragment uses two parameters:

  • tag (required) - unique fragment identifier
  • id (optional) - widget instance identifier

Fragments must be registered in DuitRegistry before use. When processing the widget, the Fragment will be replaced with the content associated with the specified tag.

import { DuitView, Column, Container, Text, Fragment } from "duit_js";

function headerFragment() {
return Container({
attributes: {
color: "#4285F4",
padding: 16,
}
}).addChild(
Text({
attributes: {
data: "Welcome to Duit!",
style: {
fontSize: 24,
color: "#FFFFFF",
}
}
})
);
}

function mainScreen() {
const builder = DuitView.builder();

// Регистрируем фрагмент
builder.addComponents([
{
tag: "header",
layoutRoot: headerFragment()
}
]);

const root = builder.createRootOfExactType(DuitElementType.column, {
mainAxisAlignment: "start"
});

root.addChildren([
// Используем зарегистрированный фрагмент
Fragment("header", "header-1"),
Container({
attributes: {
padding: 16,
}
}).addChild(
Text({
attributes: {
data: "Main content goes here",
}
})
)
]);

return builder.build();
}

Fragment Registration​

Fragments must be registered before they can be used in the widget tree. This is done through the embedded field in the server response structure.

Data Structure​

A fragment is described by an object with the following fields:

  • tag - unique fragment identifier
  • layoutRoot - widget structure contained within the fragment

Example with Multiple Fragments​

import { 
DuitView,
Column,
Row,
Container,
Text,
SizedBox,
Fragment,
DuitElementType
} from "duit_js";

function cardFragment() {
return Container({
attributes: {
color: "#F5F5F5",
borderRadius: 8,
padding: 12,
boxShadow: [{
color: "#CCCCCC",
blurRadius: 4,
}]
}
}).addChild(
Column({
attributes: {
crossAxisAlignment: "start",
}
}).addChildren([
Text({
attributes: {
data: "Card Title",
style: { fontSize: 18, fontWeight: 600 }
}
}),
SizedBox({ attributes: { height: 8 } }),
Text({
attributes: {
data: "Card description goes here",
style: { fontSize: 14, color: "#666666" }
}
})
])
);
}

function sidebarFragment() {
return Container({
attributes: {
width: 200,
color: "#2C3E50",
padding: 16,
}
}).addChild(
Column({
attributes: {
mainAxisAlignment: "start",
}
}).addChildren([
Text({
attributes: {
data: "Navigation",
style: {
fontSize: 20,
fontWeight: 700,
color: "#FFFFFF"
}
}
}),
SizedBox({ attributes: { height: 16 } }),
// ... navigation items
])
);
}

function complexLayout() {
const builder = DuitView.builder();

// Регистрируем несколько фрагментов
builder.addComponents([
{
tag: "card",
layoutRoot: cardFragment()
},
{
tag: "sidebar",
layoutRoot: sidebarFragment()
}
]);

const root = builder.createRootOfExactType(
DuitElementType.row,
{}
);

root.addChildren([
// Используем фрагмент сайдбара
Fragment("sidebar", "sidebar-1"),
// Используем фрагмент карточки
Fragment("card", "card-1"),
SizedBox({ attributes: { width: 16 } }),
Fragment("card", "card-2"),
]);

return builder.build();
}

Important Notes​

  1. Tag is Required: The tag parameter is mandatory. If it is not specified, the function will cause a panic (Go) or an error (TypeScript).

  2. Registration Before Use: Fragments must be registered before they are used in the widget tree.

  3. Reusability: The same fragment can be used multiple times with different ids.

  4. Static Content: Fragment is intended for static content. For dynamic data, use Component.

Differences Between Fragment and Component​

CharacteristicFragmentComponent
DataStatic contentDynamic content
ParametersOnly tag and idtag, id and data
UsageCaching and decompositionReusability with data

When to Use Fragment​

Fragment should be used when:

  • You have a static UI part that repeats on the screen
  • You need to optimize performance through preprocessing
  • You need to decompose a large screen into reusable parts
  • The content does not depend on runtime data

For dynamic components with data, use Component.