Gathering detailed insights and metrics for @preact/signals-core
Gathering detailed insights and metrics for @preact/signals-core
Gathering detailed insights and metrics for @preact/signals-core
Gathering detailed insights and metrics for @preact/signals-core
Manage state with style in every framework
npm install @preact/signals-core
Typescript
Module System
Node Version
NPM Version
100
Supply Chain
100
Quality
91.1
Maintenance
100
Vulnerability
100
License
@preact/signals-core@1.12.1
Updated on Aug 23, 2025
@preact/signals-preact-transform@0.1.0
Updated on Aug 20, 2025
@preact/signals@2.3.1
Updated on Aug 20, 2025
@preact/signals-debug@1.0.0
Updated on Aug 20, 2025
@preact/signals@2.2.1
Updated on Jun 29, 2025
@preact/signals-react@3.2.1
Updated on Jun 29, 2025
TypeScript (90.09%)
JavaScript (7.14%)
CSS (2.14%)
HTML (0.58%)
Dockerfile (0.04%)
Shell (0.02%)
Total Downloads
28,403,705
Last Day
28,731
Last Week
631,303
Last Month
2,728,056
Last Year
19,750,620
MIT License
4,209 Stars
882 Commits
116 Forks
26 Watchers
34 Branches
60 Contributors
Updated on Sep 02, 2025
Latest Version
1.12.1
Package Id
@preact/signals-core@1.12.1
Unpacked Size
197.93 kB
Size
52.06 kB
File Count
14
NPM Version
10.8.2
Node Version
18.20.8
Published on
Aug 23, 2025
Cumulative downloads
Total Downloads
Last Day
-13%
28,731
Compared to previous day
Last Week
-5.8%
631,303
Compared to previous week
Last Month
7.9%
2,728,056
Compared to previous month
Last Year
183.3%
19,750,620
Compared to previous year
No dependencies detected.
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.
1npm install @preact/signals-core
The signals library exposes five 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.
You can also pass options to signal()
and computed()
to be notified when the signal gains its first subscriber and loses its last subscriber:
1const counter = signal(0, { 2 name: "counter", 3 watched: function () { 4 console.log("Signal has its first subscriber"); 5 }, 6 unwatched: function () { 7 console.log("Signal lost its last subscriber"); 8 }, 9});
These callbacks are useful for managing resources or side effects that should only be active when the signal has subscribers. For example, you might use them to start/stop expensive background operations or subscribe/unsubscribe from external event sources.
The name
option will be used in the @preact/signals-debug
package to provide meaningful names for the log output.
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";
Alternatively, you can also declare your effect as a non-arrow function and call dispose
on the this
.
1effect(function () { 2 this.dispose(); 3});
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.