eleva

Best Practices

Version: 1.0.0 Comprehensive guide to writing efficient, maintainable Eleva components.

Overview

This guide covers best practices organized by topic:

Guide Topics Covered
Selectors & Structure Selector performance, component property order
Setup & Lifecycle Setup patterns, lifecycle hooks, cleanup
Signals & Templates Signal usage, template syntax, children, communication
Performance Render optimization, large lists, virtual scrolling

Quick Reference: Do’s and Don’ts

Do’s

Don’ts


Performance Tips Summary

  1. Batch signal updates - Multiple updates in one function
  2. Use memoization - Cache expensive computations
  3. Lazy load components - Load on demand when possible
  4. Keep templates simple - Complex logic in setup, not template
  5. Minimize DOM queries - Use efficient selectors
  6. Use keyed reconciliation for lists - Add key attribute

Quick Syntax Reference

Template Context Rules

Quick Rule: ${} needs ctx.@events and :props don’t.

template: (ctx) => `
  <!-- ${} interpolation needs ctx. -->
  <p>${ctx.count.value}</p>

  <!-- @events don't need ctx. -->
  <button @click="handleClick">Click</button>

  <!-- :props don't need ctx. -->
  <child :data="items.value"></child>
`

Signal Updates

// Primitives - direct assignment
count.value++;

// Objects - replace entire object
user.value = { ...user.value, name: "Jane" };

// Arrays - replace with new array
items.value = [...items.value, newItem];  // Add
items.value = items.value.filter(i => i.id !== id);  // Remove

Event Handlers with Parameters

// Without params - reference directly
<button @click="handleClick">Click</button>

// With params - use arrow function
<button @click="() => removeItem(${item.id})">Remove</button>

Next Steps


← Back to Patterns Selectors & Structure →