createSignal
Create a reactive signal (Signal) that notifies subscribers when value changes.
API
createSignal
Return
| Argument | Description | Type |
|---|---|---|
[getter, setter] | Returns getter and setter functions | [() => T, (newValue: T) => void] |
Parameters
| Parameter | Description | Type | Default |
|---|---|---|---|
value | Initial value | T | Required |
options | Configuration options (optional) | Options | Optional |
Options
| Parameter | Description | Type | Default |
|---|---|---|---|
subscriber | Subscriber identifier | string | Optional |
equals | Equality comparison function or boolean | boolean | Function | Optional |
Example
Basic Usage
js
import { createSignal } from 'ranuts';
const [count, setCount] = createSignal(0);
console.log(count()); // 0
setCount(10);
console.log(count()); // 10Subscribe to Changes
js
import { createSignal, subscribers } from 'ranuts';
const [name, setName] = createSignal('John', {
subscriber: 'nameSignal',
});
// Subscribe to changes
subscribers.tap('nameSignal', () => {
console.log('Name changed:', name());
});
setName('Jane'); // Triggers subscription callbackCustom Comparison Function
js
import { createSignal } from 'ranuts';
const [user, setUser] = createSignal(
{ id: 1, name: 'John' },
{
equals: (prev, next) => prev.id === next.id,
},
);
// Only updates when id is different
setUser({ id: 1, name: 'Jane' }); // Won't update (same id)
setUser({ id: 2, name: 'Bob' }); // Will update (different id)Disable Auto Comparison
js
import { createSignal } from 'ranuts';
const [data, setData] = createSignal(
{ value: 1 },
{
equals: false, // Always updates, no comparison
},
);Notes
- Reactive: Automatically notifies subscribers when value changes (if
subscriberis set). - Deep comparison: Defaults to using
isEqualfor deep comparison, only updates when value truly changes. - Custom comparison: Can customize comparison logic through
equalsoption. - Use case: Commonly used for state management, reactive UI, data binding, etc.