Description: Achieve computed properties using JavaScript getter functions within x-data or Alpine.data(). These re-evaluate automatically when their dependencies change, allowing you to derive state while keeping your primary state clean and focused.
get propertyName() { ... } (in x-data or Alpine.data):
This is standard JavaScript syntax for defining a getter method on an object. When used within an Alpine.js component's data object (either directly in an x-data attribute or within the object returned by Alpine.data()), Alpine.js enhances these getters by making them reactive.
For example:
// Inside Alpine.data()
{
firstName: 'John',
lastName: 'Doe',
get fullName() {
return this.firstName + ' ' + this.lastName;
}
}
In this case, fullName is a derived property. You would access it like a regular property (e.g., this.fullName in JavaScript or fullName in your HTML template), but its value is dynamically calculated by the getter function.
Reactive Dependencies:
Alpine.js intelligently tracks which reactive state properties are accessed *inside* a getter function. These accessed properties are considered "dependencies" of the getter. Whenever any of these dependencies change, Alpine.js automatically re-evaluates the getter function and updates any parts of your HTML template that use the getter's result.
In the fullName example above, firstName and lastName are reactive dependencies. If either this.firstName or this.lastName changes, the fullName getter will automatically re-run, and any HTML element displaying fullName will update to reflect the new combined name.
This is powerful because it allows you to define complex derived state without manually setting up listeners or update logic. Alpine handles the reactivity for you.
Getters are re-evaluated on dependency change. Avoid overly expensive operations directly in getters if they update very frequently:
While efficient, if a getter performs very complex calculations (e.g., iterating over thousands of items, complex mathematical operations) and its dependencies change many times per second (e.g., rapid input from a user), it could potentially impact performance in extreme cases. If you encounter such scenarios, consider strategies like:
For most common use cases, like filtering a reasonably sized list or concatenating strings, Alpine's getters are perfectly performant.
Getters should not have side effects; their purpose is to derive and return a value:
A getter's sole responsibility is to compute and return a value based on existing state. Modifying other state properties from within a getter (e.g., this.someOtherProperty = 'new value'; inside a getter) is an anti-pattern. This can lead to:
Think of getters as read-only computed values. If you need to perform an action that changes state, use a regular method in your Alpine component, typically triggered by an event like @click or @input.
Showing of items. Total value of selected items: $
| Name | Category | Price | Quantity | Subtotal |
|---|---|---|---|---|
No Results
No items match your search term "".
All Items
Currently showing all items. Use the search box to filter.