Idioms and Patterns

Element's reactive system and template-based approach enable powerful patterns for building modern Web applications. This guide covers common idioms, best practices, and advanced techniques for getting the most out of Element.

Reactive Patterns

Understanding how to effectively use Element's reactive system is key to building maintainable and performant applications.

Derived State

Derived state is computed from other signals and automatically updates when dependencies change. This pattern is fundamental to creating clean, declarative UIs.

// Computed with conditional dependencies
const [withEmail, setWithEmail] = createSignal(false);
const [user, setUser] = createSignal({
  name: "John Doe",
  email: "john.doe@pm.me",
});

const author = createComputed(() => {
  if (withEmail()) {
    return `${user().name} <${user().email}>`;
  }
  return user().name;
});

Conditional Effects

Resource Loading Pattern

A common pattern for managing asynchronous resources with loading states and error handling.

Nested Batching

Advanced Template Features

Element's template system provides sophisticated features for building complex UIs while maintaining reactivity and performance.

Nested Components

Components can be nested and composed to create reusable UI elements.

Last updated