
Enter a search term above to see results...
Enter a search term above to see results...
Dependent signals are signals whose values automatically update when other signals change. Semantic UI provides two methods for creating dependent signals:
signal.derive()
- Transform a single signal into a new signalSignal.computed()
- Combine multiple signals into a computed valueUse derive()
to create a new signal that transforms the value of a single source signal. The derived signal automatically updates whenever the source signal changes.
import { Signal } from '@semantic-ui/reactivity';
const numbers = new Signal([1, 2, 3, 4, 5]);
// Derive the array lengthconst count = numbers.derive(arr => arr.length);
console.log(count.get()); // 5
// When numbers changes, count updates automaticallynumbers.push(6);console.log(count.get()); // 6
Use derive()
when you need to:
const users = new Signal([ { name: 'Alice', active: true }, { name: 'Bob', active: false }]);
// Extract active usersconst activeUsers = users.derive(list => list.filter(user => user.active));
// Get user names as a stringconst userNames = users.derive(list => list.map(user => user.name).join(', '));
Use Signal.computed()
to create a signal that depends on multiple other signals. The computed signal recalculates whenever any of its dependencies change.
import { Signal } from '@semantic-ui/reactivity';
const firstName = new Signal('John');const lastName = new Signal('Doe');
// Compute full name from multiple signalsconst fullName = Signal.computed(() => `${firstName.get()} ${lastName.get()}`);
console.log(fullName.get()); // "John Doe"
// When either signal changes, fullName updates automaticallyfirstName.set('Jane');console.log(fullName.get()); // "Jane Doe"
Use Signal.computed()
when you need to:
const quantity = new Signal(3);const price = new Signal(10.99);const taxRate = new Signal(0.08);
// Calculate total from multiple sourcesconst total = Signal.computed(() => { const subtotal = quantity.get() * price.get(); const tax = subtotal * taxRate.get(); return subtotal + tax;});
// Conditional computationconst isExpensive = Signal.computed(() => total.get() > 50);
Both derived and computed signals work seamlessly with reactions, allowing you to create reactive side effects based on dependent values.
import { Signal, Reaction } from '@semantic-ui/reactivity';
const items = new Signal(['apple', 'banana']);const itemCount = items.derive(arr => arr.length);
// React to changes in derived signalReaction.create(() => { console.log(`You have ${itemCount.get()} items`);});
// Adding items triggers the reactionitems.push('cherry'); // Logs: "You have 3 items"
Performance Both
derive()
andcomputed()
create efficient reactive dependencies that only update when their source values actually change, thanks to Semantic UI’s built-in equality checking.