Advanced Computed Properties

Template expressions, chained computed arrays, filtering/sorting patterns, and best practices for keeping computed properties fast and correct.

Computed Properties in Templates

Use computed properties in data binding by name. No prefix needed. WildflowerJS automatically detects computed properties:

💡 Tip: Just use data-bind="fullName". The computed: prefix is optional and unnecessary. Computed properties always resolve by bare name.
⚠️ Precedence Rule: If both state and computed have a property with the same name, the computed property takes precedence. This allows computed properties to "override" or transform state values when needed.

Computed Properties in Expressions

Expression namespace: Computed properties are automatically merged into the expression namespace alongside state properties. This means you can use computed values directly in any expression: conditionals, ternaries, string concatenation, comparisons, and more.

Unlike some frameworks where computed values require special syntax or function calls, WildflowerJS treats computed properties as first-class citizens:

wildflower.component('user-dashboard', {
    state: {
        firstName: 'Alice',
        lastName: 'Smith',
        items: [1, 2, 3],
        threshold: 5
    },
    computed: {
        fullName() { return `${this.firstName} ${this.lastName}`; },
        itemCount() { return this.items.length; },
        hasItems() { return this.items.length > 0; },
        isOverThreshold() { return this.items.length > this.threshold; }
    }
});
<!-- Computed values work directly in expressions -->

<!-- String concatenation with computed + state -->
<p data-bind="fullName + ' has ' + itemCount + ' items'"></p>

<!-- Ternary expressions mixing computed and state -->
<p data-bind="hasItems ? 'You have ' + itemCount + ' items' : 'No items yet'"></p>

<!-- Boolean logic in conditionals -->
<div data-show="hasItems && itemCount > 2">Many items!</div>
<div data-show="isOverThreshold || itemCount === 0">Edge case</div>

<!-- Computed values in comparisons -->
<span data-bind="itemCount >= threshold ? 'At capacity' : 'Room for more'"></span>

<!-- Computed in class bindings -->
<div data-bind-class="{ 'has-content': hasItems, 'empty': !hasItems, 'over-limit': isOverThreshold }"></div>

<!-- Computed in style bindings -->
<div data-bind-style="{ opacity: hasItems ? '1' : '0.5' }"></div>

This is comparable to Vue's computed + template expressions, Svelte 5's $derived, and Solid's createMemo, but without requiring a build step or special syntax. The computed values simply exist as variables in your expressions.

Binding Type Example Description
Text Content data-bind="fullName" Display computed value as text
HTML Content data-bind-html="formattedContent" Insert computed HTML content
Class Binding data-bind-class="statusClass" Set CSS classes from computed string
Style Binding data-bind-style="dynamicStyles" Set inline styles from computed object
Conditionals data-show="isVisible" Show/hide based on computed boolean
Lists data-list="filteredItems" Render computed arrays

Chaining, Filtering & Sorting

Explore advanced patterns like chaining computed properties, filtering and sorting arrays, and item-level computed values:

<div>
    <!-- Task Manager Component -->
    <div data-component="task-dashboard" class="mb-4">
        <div class="row">
            <div class="col-md-6">
                <h5>Task Management</h5>

                <div class="mb-3">
                    <input type="text"
                           class="form-control mb-2"
                           data-model="newTaskText"
                           placeholder="Add a new task...">
                    <button class="btn btn-primary btn-sm me-2" data-action="addTask">
                        Add Task
                    </button>
                    <button class="btn btn-secondary btn-sm" data-action="addSampleTasks">
                        Add Sample Tasks
                    </button>
                </div>

                <div class="mb-3">
                    <label class="form-label">Filter Tasks:</label>
                    <select class="form-select" data-model="currentFilter">
                        <option value="all">All Tasks</option>
                        <option value="active">Active Only</option>
                        <option value="completed">Completed Only</option>
                        <option value="priority">High Priority</option>
                    </select>
                </div>

                <div class="mb-3">
                    <label class="form-label">Sort By:</label>
                    <select class="form-select" data-model="sortBy">
                        <option value="created">Date Created</option>
                        <option value="priority">Priority</option>
                        <option value="alphabetical">Alphabetical</option>
                    </select>
                </div>
            </div>

            <div class="col-md-6">
                <h5>Computed Statistics</h5>
                <div class="card"><div class="card-body">
                    <p><strong>Total Tasks:</strong> <span data-bind="totalTasks"></span></p>
                    <p><strong>Active Tasks:</strong> <span data-bind="activeTasks"></span></p>
                    <p><strong>Completed:</strong> <span data-bind="completedTasks"></span></p>
                    <p><strong>Completion Rate:</strong> <span data-bind="completionRate"></span>%</p>
                    <p><strong>High Priority:</strong> <span data-bind="highPriorityCount"></span></p>
                    <p><strong>Status:</strong> <span data-bind="statusMessage" class="badge bg-info"></span></p>
                </div></div>
            </div>
        </div>

        <h5>Filtered & Sorted Tasks (<span data-bind="filteredTaskCount"></span> shown)</h5>
        <div data-list="sortedAndFilteredTasks" class="mb-3">
            <template>
                <div class="d-flex align-items-center mb-2 p-2 border rounded"
                     data-bind-class="completed ? 'bg-light text-muted' : ''">
                    <input type="checkbox"
                           class="form-check-input me-2"
                           data-model="completed">
                    <div class="flex-grow-1">
                        <span data-bind="text"></span>
                        <small class="text-muted d-block">
                            Priority: <span data-bind="priority"></span> |
                            Created: <span data-bind="formattedDate"></span>
                        </small>
                    </div>
                    <span data-bind-class="priorityBadgeClass" class="badge me-2">
                        <span data-bind="priority"></span>
                    </span>
                    <button class="btn btn-sm btn-danger" data-action="removeTask">×</button>
                </div>
            </template>
        </div>

        <div data-show="noTasksVisible" class="text-center text-muted py-3">
            No tasks match the current filter.
        </div>
    </div>
</div>
wildflower.component('task-dashboard', {
    state: {
        tasks: [],
        newTaskText: '',
        currentFilter: 'all',
        sortBy: 'created'
    },

    computed: {
        // Basic computed properties
        totalTasks() {
            return this.tasks.length
        },

        activeTasks() {
            return this.tasks.filter(task => !task.completed).length
        },

        completedTasks() {
            return this.tasks.filter(task => task.completed).length
        },

        highPriorityCount() {
            return this.tasks.filter(task => task.priority === 'high').length
        },

        // Chained computed properties
        completionRate() {
            return this.totalTasks > 0
                ? Math.round((this.completedTasks / this.totalTasks) * 100)
                : 0
        },

        statusMessage() {
            if (this.totalTasks === 0) return 'No tasks'
            if (this.completionRate === 100) return 'All complete!'
            if (this.completionRate >= 75) return 'Almost done!'
            if (this.completionRate >= 50) return 'Good progress'
            return 'Getting started'
        },

        // Computed array filtering
        filteredTasks() {
            const filter = this.currentFilter
            const tasks = this.tasks

            switch (filter) {
                case 'active':
                    return tasks.filter(task => !task.completed)
                case 'completed':
                    return tasks.filter(task => task.completed)
                case 'priority':
                    return tasks.filter(task => task.priority === 'high')
                default:
                    return tasks
            }
        },

        // Computed array sorting (depends on filteredTasks)
        sortedAndFilteredTasks() {
            const tasks = [...this.filteredTasks]
            const sortBy = this.sortBy

            switch (sortBy) {
                case 'priority':
                    const priorityOrder = { 'high': 3, 'medium': 2, 'low': 1 }
                    return tasks.sort((a, b) => priorityOrder[b.priority] - priorityOrder[a.priority])
                case 'alphabetical':
                    return tasks.sort((a, b) => a.text.localeCompare(b.text))
                default: // created
                    return tasks.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
            }
        },

        filteredTaskCount() {
            return this.filteredTasks.length
        },

        noTasksVisible() {
            return this.sortedAndFilteredTasks.length === 0
        },

        // Item-level computeds: note the `(item)` parameter. The framework
        // uses fn.length > 0 to detect these and runs them once per list item
        // with the current item as the argument. See the dedicated section on
        // /docs/lists for compound-expression usage.
        formattedDate(item) {
            return new Date(item.createdAt).toLocaleDateString()
        },

        priorityBadgeClass(item) {
            const classes = {
                'high': 'bg-danger',
                'medium': 'bg-warning',
                'low': 'bg-success'
            }
            return classes[item.priority] || 'bg-secondary'
        }
    },

    init() {
        this._nextId = 0
    },

    addTask() {
        const text = this.newTaskText.trim()
        if (!text) return

        const priorities = ['low', 'medium', 'high']

        this.tasks.push({
            id: ++this._nextId,
            text: text,
            completed: false,
            priority: priorities[Math.floor(Math.random() * priorities.length)],
            createdAt: new Date().toISOString()
        })

        this.newTaskText = ''
    },

    addSampleTasks() {
        const sampleTasks = [
            { text: 'Complete project documentation', priority: 'high' },
            { text: 'Review pull requests', priority: 'medium' },
            { text: 'Update dependencies', priority: 'low' },
            { text: 'Fix critical bug in production', priority: 'high' },
            { text: 'Plan team meeting', priority: 'medium' }
        ]

        sampleTasks.forEach(task => {
            this.tasks.push({
                id: ++this._nextId,
                ...task,
                completed: Math.random() > 0.7,
                createdAt: new Date(Date.now() - Math.random() * 7 * 24 * 60 * 60 * 1000).toISOString()
            })
        })
    },

    removeTask(event, element, details) {
        // details.index is the position in the rendered (filtered/sorted) list,
        // so look up the actual item and find it in the original array
        const task = this.sortedAndFilteredTasks[details.index]
        const originalIndex = this.tasks.indexOf(task)
        if (originalIndex !== -1) this.tasks.splice(originalIndex, 1)
    }
})
Live Preview

Best Practices

✅ Do
  • Keep computed properties pure (no side effects)
  • Use computed properties for derived state
  • Cache expensive calculations in computed properties
  • Use descriptive names for computed properties
  • Return consistent data types
  • Track side effects outside computed properties
❌ Don't
  • Modify state within computed properties
  • Make API calls in computed properties
  • Use computed properties for actions/events
  • Create circular dependencies
  • Access DOM elements directly
  • Update reactive state synchronously in computed properties

Avoiding Circular Dependencies

A common pitfall is modifying state inside a computed property that depends on that same state:

⚠️ Circular Dependency
// ❌ Bad - Creates infinite loop
computed: {
    expensiveCalculation() {
        this.calculationCount++  // Modifies state!
        return this.data.reduce((a, b) => a + b, 0)
    }
}

Computed runs → modifies state → triggers reactivity → computed runs again → infinite loop.

✅ Solution: Use a Watcher
// ✅ Good - Computed stays pure, watcher handles the side effect
computed: {
    expensiveCalculation() {
        return this.data.reduce((a, b) => a + b, 0)
    }
},

watch: {
    data() {
        this.calculationCount++
    }
}

Keep computed properties pure, with no state mutations or side effects. If you need to react when a value changes, use watch instead.