Reactive variables
Computed
Reactivity
Automatically calculate progress percentage with computed variables, map and filter.
Getting Started with a real app (Episode 5)
Back to the Academy
Reactive variables
Express Summary: Automatically calculate progress percentage with computed variables, map and filter for real-time reactivity.
Introduction to computed variables
We're going to create a progress indicator that displays the project advancement percentage. Concretely: number of completed tasks / total number of tasks � 100.
Why a computed variable?
Unlike regular variables, computed variables automatically recalculate when data changes. Perfect for a percentage that needs to be updated with each task addition or modification.
1. Create the computed variable
Basic configuration
- Create a computed variable named "percentage"
- Display it in the interface (for example in parentheses in the header)
- Access the Logic tab to define the calculation
2. Calculate total number of tasks
Retrieve store data
- Use get store
- Spread the store to access properties
- Use get property to retrieve "cards"
Use the Map function
The Map function executes a function on each array element.
- Connect the card array to a Map block
- Create a lambda function (anonymous function)
- In this function:
- Retrieve current element
- Use get property to get "tasks"
- Use get length to count tasks
- Connect to computed execution
Sum all results
- Map returns an array of numbers (number of tasks per card)
- Use Sum to add all elements
- Result: total number of tasks in the application
3. Calculate number of completed tasks
Filter "Done" cards
- Retrieve store and cards again
- Use Filter instead of Map
- Filter keeps only elements that pass a test
Configure filtering test
- In Filter's lambda function:
- Use get property to retrieve "title"
- Test if title equals "Done"
- Callback becomes red (boolean) because it's a test
- Connect to execution (after Map)
Count tasks in "Done" card
- Filter returns an array with one element (the "Done" card)
- Use hat (index 0) to retrieve the first element
- Get property � "tasks" � get length
- Result: number of completed tasks
4. Calculate percentage
Division and formatting
- Use Divide: completed tasks � total tasks
- Multiply by 100 to get percentage
- Round to round the result
- Connect final result to percentage variable
Deep function for arrays
� Important: Since we use arrays, add the deep function to the computed to monitor deep changes.
Complete logical structure
The complete data flow:
- Store � cards � Map � lengths � Sum = total tasks
- Store � cards � Filter � "Done" card � Hat[0] � tasks � Length = completed tasks
- Divide � Multiply 100 � Round = final percentage
Key concepts
Computed variables
- Automatic reactivity: recalculation on each dependency change
- Optimized performance: cached until next change
- Declarative: describe what we want, not how to get it
Advanced array functions
- Map: transformation of each element
- Filter: conditional selection of elements
- Sum: aggregation of numeric values
- Hat[index]: access to specific element
Deep monitoring
- deep: necessary to monitor changes in objects/arrays
- Without deep: only references are monitored
- With deep: all internal changes are detected
Advantages of this approach
- Real-time: indicator updates instantly
- Automatic: no manual action required
- Performant: recalculation only when necessary
- Maintainable: centralized and reusable logic
Coming next: Organize and structure code with reusable functions.