Gathering detailed insights and metrics for @preact/signals
Gathering detailed insights and metrics for @preact/signals
Gathering detailed insights and metrics for @preact/signals
Gathering detailed insights and metrics for @preact/signals
@lit-labs/preact-signals
Preact Signals integration for Lit
@preact-signals/utils
Utils for @preact/signals
deepsignal
Use [Preact signals](https://github.com/preactjs/signals) with the interface of a plain JavaScript object.
@dxos/echo-signals
ECHO object reactivity integration with Preact Signals
npm install @preact/signals
@preact/signals@1.3.1
Published on 27 Nov 2024
@preact/signals-react@2.2.0
Published on 08 Aug 2024
@preact/signals-core@1.8.0
Published on 04 Aug 2024
@preact/signals-react-transform@0.4.0
Published on 23 Jul 2024
@preact/signals-react-transform@0.3.2
Published on 06 Jul 2024
@preact/signals-react@2.1.0
Published on 06 Jul 2024
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,828 Stars
794 Commits
95 Forks
27 Watching
32 Branches
54 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (94.75%)
JavaScript (5.17%)
Dockerfile (0.06%)
Shell (0.03%)
Cumulative downloads
Total Downloads
Last day
3.7%
14,059
Compared to previous day
Last week
3.3%
76,337
Compared to previous week
Last month
-18.8%
329,883
Compared to previous month
Last year
235.2%
3,895,938
Compared to previous year
1
1
2
Signals is a performant state management library with two primary goals:
Read the announcement post to learn more about which problems signals solves and how it came to be.
1# Just the core library 2npm install @preact/signals-core 3# If you're using Preact 4npm install @preact/signals 5# If you're using React 6npm install @preact/signals-react 7# If you're using Svelte 8npm install @preact/signals-core
The signals library exposes four functions which are the building blocks to model any business logic you can think of.
signal(initialValue)
The signal
function creates a new signal. A signal is a container for a value that can change over time. You can read a signal's value or subscribe to value updates by accessing its .value
property.
1import { signal } from "@preact/signals-core"; 2 3const counter = signal(0); 4 5// Read value from signal, logs: 0 6console.log(counter.value); 7 8// Write to a signal 9counter.value = 1;
Writing to a signal is done by setting its .value
property. Changing a signal's value synchronously updates every computed and effect that depends on that signal, ensuring your app state is always consistent.
signal.peek()
In the rare instance that you have an effect that should write to another signal based on the previous value, but you don't want the effect to be subscribed to that signal, you can read a signals's previous value via signal.peek()
.
1const counter = signal(0); 2const effectCount = signal(0); 3 4effect(() => { 5 console.log(counter.value); 6 7 // Whenever this effect is triggered, increase `effectCount`. 8 // But we don't want this signal to react to `effectCount` 9 effectCount.value = effectCount.peek() + 1; 10});
Note that you should only use signal.peek()
if you really need it. Reading a signal's value via signal.value
is the preferred way in most scenarios.
computed(fn)
Data is often derived from other pieces of existing data. The computed
function lets you combine the values of multiple signals into a new signal that can be reacted to, or even used by additional computeds. When the signals accessed from within a computed callback change, the computed callback is re-executed and its new return value becomes the computed signal's value.
1import { signal, computed } from "@preact/signals-core"; 2 3const name = signal("Jane"); 4const surname = signal("Doe"); 5 6const fullName = computed(() => name.value + " " + surname.value); 7 8// Logs: "Jane Doe" 9console.log(fullName.value); 10 11// Updates flow through computed, but only if someone 12// subscribes to it. More on that later. 13name.value = "John"; 14// Logs: "John Doe" 15console.log(fullName.value);
Any signal that is accessed inside the computed
's callback function will be automatically subscribed to and tracked as a dependency of the computed signal.
effect(fn)
The effect
function is the last piece that makes everything reactive. When you access a signal inside its callback function, that signal and every dependency of said signal will be activated and subscribed to. In that regard it is very similar to computed(fn)
. By default all updates are lazy, so nothing will update until you access a signal inside effect
.
1import { signal, computed, effect } from "@preact/signals-core"; 2 3const name = signal("Jane"); 4const surname = signal("Doe"); 5const fullName = computed(() => name.value + " " + surname.value); 6 7// Logs: "Jane Doe" 8effect(() => console.log(fullName.value)); 9 10// Updating one of its dependencies will automatically trigger 11// the effect above, and will print "John Doe" to the console. 12name.value = "John";
You can destroy an effect and unsubscribe from all signals it was subscribed to, by calling the returned function.
1import { signal, computed, effect } from "@preact/signals-core"; 2 3const name = signal("Jane"); 4const surname = signal("Doe"); 5const fullName = computed(() => name.value + " " + surname.value); 6 7// Logs: "Jane Doe" 8const dispose = effect(() => console.log(fullName.value)); 9 10// Destroy effect and subscriptions 11dispose(); 12 13// Update does nothing, because no one is subscribed anymore. 14// Even the computed `fullName` signal won't change, because it knows 15// that no one listens to it. 16surname.value = "Doe 2";
The effect callback may return a cleanup function. The cleanup function gets run once, either when the effect callback is next called or when the effect gets disposed, whichever happens first.
1import { signal, effect } from "@preact/signals-core"; 2 3const count = signal(0); 4 5const dispose = effect(() => { 6 const c = count.value; 7 return () => console.log(`cleanup ${c}`); 8}); 9 10// Logs: cleanup 0 11count.value = 1; 12 13// Logs: cleanup 1 14dispose();
batch(fn)
The batch
function allows you to combine multiple signal writes into one single update that is triggered at the end when the callback completes.
1import { signal, computed, effect, batch } from "@preact/signals-core"; 2 3const name = signal("Jane"); 4const surname = signal("Doe"); 5const fullName = computed(() => name.value + " " + surname.value); 6 7// Logs: "Jane Doe" 8effect(() => console.log(fullName.value)); 9 10// Combines both signal writes into one update. Once the callback 11// returns the `effect` will trigger and we'll log "Foo Bar" 12batch(() => { 13 name.value = "Foo"; 14 surname.value = "Bar"; 15});
When you access a signal that you wrote to earlier inside the callback, or access a computed signal that was invalidated by another signal, we'll only update the necessary dependencies to get the current value for the signal you read from. All other invalidated signals will update at the end of the callback function.
1import { signal, computed, effect, batch } from "@preact/signals-core"; 2 3const counter = signal(0); 4const double = computed(() => counter.value * 2); 5const triple = computed(() => counter.value * 3); 6 7effect(() => console.log(double.value, triple.value)); 8 9batch(() => { 10 counter.value = 1; 11 // Logs: 2, despite being inside batch, but `triple` 12 // will only update once the callback is complete 13 console.log(double.value); 14}); 15// Now we reached the end of the batch and call the effect
Batches can be nested and updates will be flushed when the outermost batch call completes.
1import { signal, computed, effect, batch } from "@preact/signals-core"; 2 3const counter = signal(0); 4effect(() => console.log(counter.value)); 5 6batch(() => { 7 batch(() => { 8 // Signal is invalidated, but update is not flushed because 9 // we're still inside another batch 10 counter.value = 1; 11 }); 12 13 // Still not updated... 14}); 15// Now the callback completed and we'll trigger the effect.
untracked(fn)
In case when you're receiving a callback that can read some signals, but you don't want to subscribe to them, you can use untracked
to prevent any subscriptions from happening.
1const counter = signal(0); 2const effectCount = signal(0); 3const fn = () => effectCount.value + 1; 4 5effect(() => { 6 console.log(counter.value); 7 8 // Whenever this effect is triggered, run `fn` that gives new value 9 effectCount.value = untracked(fn); 10});
MIT
, see the LICENSE file.
No vulnerabilities found.
No security vulnerabilities found.