Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@preact/signals-core

preactjs2.4mMIT1.10.0TypeScript support: included

Manage state with style in every framework

readme

Signals

Signals is a performant state management library with two primary goals:

  1. Make it as easy as possible to write business logic for small up to complex apps. No matter how complex your logic is, your app updates should stay fast without you needing to think about it. Signals automatically optimize state updates behind the scenes to trigger the fewest updates necessary. They are lazy by default and automatically skip signals that no one listens to.
  2. Integrate into frameworks as if they were native built-in primitives. You don't need any selectors, wrapper functions, or anything else. Signals can be accessed directly and your component will automatically re-render when the signal's value changes.

Read the announcement post to learn more about which problems signals solves and how it came to be.

Installation:

npm install @preact/signals-core

Guide / API

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.

import { signal } from "@preact/signals-core";

const counter = signal(0);

// Read value from signal, logs: 0
console.log(counter.value);

// Write to a signal
counter.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:

const counter = signal(0, {
    watched: function () {
        console.log("Signal has its first subscriber");
    },
    unwatched: function () {
        console.log("Signal lost its last subscriber");
    },
});

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.

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().

const counter = signal(0);
const effectCount = signal(0);

effect(() => {
    console.log(counter.value);

    // Whenever this effect is triggered, increase `effectCount`.
    // But we don't want this signal to react to `effectCount`
    effectCount.value = effectCount.peek() + 1;
});

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.

import { signal, computed } from "@preact/signals-core";

const name = signal("Jane");
const surname = signal("Doe");

const fullName = computed(() => name.value + " " + surname.value);

// Logs: "Jane Doe"
console.log(fullName.value);

// Updates flow through computed, but only if someone
// subscribes to it. More on that later.
name.value = "John";
// Logs: "John Doe"
console.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.

import { signal, computed, effect } from "@preact/signals-core";

const name = signal("Jane");
const surname = signal("Doe");
const fullName = computed(() => name.value + " " + surname.value);

// Logs: "Jane Doe"
effect(() => console.log(fullName.value));

// Updating one of its dependencies will automatically trigger
// the effect above, and will print "John Doe" to the console.
name.value = "John";

You can destroy an effect and unsubscribe from all signals it was subscribed to, by calling the returned function.

import { signal, computed, effect } from "@preact/signals-core";

const name = signal("Jane");
const surname = signal("Doe");
const fullName = computed(() => name.value + " " + surname.value);

// Logs: "Jane Doe"
const dispose = effect(() => console.log(fullName.value));

// Destroy effect and subscriptions
dispose();

// Update does nothing, because no one is subscribed anymore.
// Even the computed `fullName` signal won't change, because it knows
// that no one listens to it.
surname.value = "Doe 2";

Alternatively, you can also declare your effect as a non-arrow function and call dispose on the this.

effect(function () {
    this.dispose();
});

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.

import { signal, effect } from "@preact/signals-core";

const count = signal(0);

const dispose = effect(() => {
    const c = count.value;
    return () => console.log(`cleanup ${c}`);
});

// Logs: cleanup 0
count.value = 1;

// Logs: cleanup 1
dispose();

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.

import { signal, computed, effect, batch } from "@preact/signals-core";

const name = signal("Jane");
const surname = signal("Doe");
const fullName = computed(() => name.value + " " + surname.value);

// Logs: "Jane Doe"
effect(() => console.log(fullName.value));

// Combines both signal writes into one update. Once the callback
// returns the `effect` will trigger and we'll log "Foo Bar"
batch(() => {
    name.value = "Foo";
    surname.value = "Bar";
});

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.

import { signal, computed, effect, batch } from "@preact/signals-core";

const counter = signal(0);
const double = computed(() => counter.value * 2);
const triple = computed(() => counter.value * 3);

effect(() => console.log(double.value, triple.value));

batch(() => {
    counter.value = 1;
    // Logs: 2, despite being inside batch, but `triple`
    // will only update once the callback is complete
    console.log(double.value);
});
// 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.

import { signal, computed, effect, batch } from "@preact/signals-core";

const counter = signal(0);
effect(() => console.log(counter.value));

batch(() => {
    batch(() => {
        // Signal is invalidated, but update is not flushed because
        // we're still inside another batch
        counter.value = 1;
    });

    // Still not updated...
});
// 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.

const counter = signal(0);
const effectCount = signal(0);
const fn = () => effectCount.value + 1;

effect(() => {
    console.log(counter.value);

    // Whenever this effect is triggered, run `fn` that gives new value
    effectCount.value = untracked(fn);
});

License

MIT, see the LICENSE file.

changelog

@preact/signals-core

1.10.0

Minor Changes

1.9.0

Minor Changes

Patch Changes

1.8.0

Minor Changes

  • #587 cd9efbb Thanks @JoviDeCroock! - Adjust the ReadOnlySignal type to not inherit from Signal this way the type can't be widened without noticing, i.e. when we'd have

    const sig: Signal = useComputed(() => x);

    We would have widened the type to be mutable again, which for a computed is not allowed. We want to provide the tools to our users to avoid these footguns hence we are correcting this type in a minor version.

1.7.0

Minor Changes

  • #578 931404e Thanks @JoviDeCroock! - Allow for passing no argument to the signal and the type to be automatically inferred as T | undefined

1.6.1

Patch Changes

  • #558 c8c95ac Thanks @jviide! - Restore stricter effect callback & cleanup function types

1.6.0

Minor Changes

Patch Changes

1.5.1

Patch Changes

1.5.0

Minor Changes

  • #405 9355d96 Thanks @JoviDeCroock! - Add unique identifier to every Signal, this will be present on the brand property of a Signal coming from either signal() or computed()

1.4.0

Minor Changes

  • #380 256a331 Thanks @XantreGodlike! - Add untracked function, this allows more granular control within effect/computed around what should affect re-runs.

1.3.1

Patch Changes

  • #373 8c12a0d Thanks @rschristian! - Removes package.json#exports.umd, which had invalid paths if they were ever to be consumed

  • #359 26f6526 Thanks @andrewiggins! - Change effect callback return type from void to unknown. Same for effect cleanup function.

1.3.0

Minor Changes

Patch Changes

1.2.3

Patch Changes

1.2.2

Patch Changes

  • #232 aa4cb7b Thanks @jviide! - Simplify effect change checking (and make effect cycle detection more accurate as a side-effect)

  • #233 3f652a7 Thanks @jviide! - Simplify Node book keeping code

1.2.1

Patch Changes

  • #205 4b73164 Thanks @jviide! - Use the same tracking logic for both effects and computeds. This ensures that effects are only called whenever any of their dependencies changes. If they all stay the same, then the effect will not be invoked.

  • #207 57fd2e7 Thanks @jviide! - Fix effect disposal when cleanup throws

  • #209 49756ae Thanks @jviide! - Optimize dependency value change checks by allowing earlier exists from the loop

1.2.0

Minor Changes

  • #183 79ff1e7 Thanks @jviide! - Add ability to run custom cleanup logic when an effect is disposed.

    effect(() => {
      console.log("This runs whenever a dependency changes");
      return () => {
        console.log("This runs when the effect is disposed");
      });
    });
  • #170 3e31aab Thanks @jviide! - Allow disposing a currently running effect

Patch Changes

  • #188 b4611cc Thanks @jviide! - Fix .subscribe() unexpectedly tracking signal access

  • #162 9802da5 Thanks @developit! - Add support for Signal.prototype.valueOf

  • #161 6ac6923 Thanks @jviide! - Remove all usages of Set, Map and other allocation heavy objects in signals-core. This substaintially increases performance across all measurements.

1.1.1

Patch Changes

1.1.0

Minor Changes

  • bc0080c: Add .subscribe()-method to signals to add support for natively using signals with Svelte

Patch Changes

  • 336bb34: Don't mangle Signal class name
  • 7228418: Fix incorrectly named variables and address typos in code comments.
  • 32abe07: Fix internal API functions being able to unmark non-invalidated signals
  • 4782b41: Fix conditionally signals (lazy branches) not being re-computed upon activation
  • bf6af3b: - Fix a memory leak when computed signals and effects are removed

1.0.1

Patch Changes

  • 5644c1f: Fix stale value returned by .peek() when called on a deactivated signal.

1.0.0

Major Changes

  • 2ee8489: The v1 release for the signals package, we'd to see the uses you all come up with and are eager to see performance improvements in your applications.

Minor Changes

  • ab22ec7: Add .peek() method to read from signals without subscribing to them.

Patch Changes

  • b56abf3: Throw an error when a cycle was detected during state updates

0.0.5

Patch Changes

  • 702a9c5: Update TypeScript types to mark computed signals as readonly

0.0.4

Patch Changes

  • 4123d60: Fix TypeScript definitions not found in core

0.0.3

Patch Changes

  • 1e4dac5: Add prepublishOnly scripts to ensure we're publishing fresh packages

0.0.2

Patch Changes

  • 2181b74: Add basic signals lib based of prototypes