# 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](https://preactjs.com/blog/introducing-signals/) to learn more about which problems signals solves and how it came to be. - [Core API](../core/README.md#guide--api) - [`signal(initialValue)`](../core/README.md#signalinitialvalue) - [`signal.peek()`](../core/README.md#signalpeek) - [`computed(fn)`](../core/README.md#computedfn) - [`effect(fn)`](../core/README.md#effectfn) - [`batch(fn)`](../core/README.md#batchfn) - [`untracked(fn)`](../core/README.md#untrackedfn) - [React Integration](#react-integration) - [Babel Transform](#babel-transform) - [`useSignals` hook](#usesignals-hook) - [Hooks](#hooks) - [Using signals with React's SSR APIs](#using-signals-with-reacts-ssr-apis) - [Rendering optimizations](#rendering-optimizations) - [Utility Components and Hooks](#utility-components-and-hooks) - [Show Component](#show-component) - [For Component](#for-component) - [Additional Hooks](#additional-hooks) - [`useLiveSignal`](#uselivesignal) - [`useSignalRef`](#usesignalref) - [Limitations](#limitations) - [License](#license) ## React Integration The React integration can be installed via: ```sh npm install @preact/signals-react ``` We have a couple of options for integrating Signals into React. The recommended approach is to use the Babel transform to automatically make your components that use signals reactive. ### Babel Transform Install the Babel transform package (`npm i --save-dev @preact/signals-react-transform`) and add the following to your Babel config: ```json { "plugins": [["module:@preact/signals-react-transform"]] } ``` This will automatically transform your components to be reactive. You can then use signals directly inside your components. ```js import { signal } from "@preact/signals-react"; const count = signal(0); function CounterValue() { // Whenever the `count` signal is updated, we'll // re-render this component automatically for you return
Value: {count.value}
; } ``` See the [Readme for the Babel plugin](../react-transform/README.md) for more details about how the transform works and configuring it. ### `useSignals` hook If you can't use the Babel transform, you can directly call the `useSignals` hook to make your components reactive. ```js import { useSignals } from "@preact/signals-react/runtime"; const count = signal(0); function CounterValue() { useSignals(); returnValue: {count.value}
; } ``` ### Hooks If you need to instantiate new signals or create new side effects on signal changes inside your components, you can use the `useSignal`, `useComputed` and `useSignalEffect` hooks. ```js import { useSignal, useComputed, useSignalEffect } from "@preact/signals-react"; function Counter() { const count = useSignal(0); const double = useComputed(() => count.value * 2); useSignalEffect(() => { console.log(`Value: ${count.value}, value x 2 = ${double.value}`); }); return ( ); } ``` ### Using signals with React's SSR APIs Components rendered using SSR APIs (e.g. `renderToString`) in a server environment (i.e. an environment without a global `window` object) will not track signals used during render. Components generally don't rerender when using SSR APIs so tracking signal usage is useless since changing these signals can't trigger a rerender. ### Rendering optimizations The React adapter ships with several optimizations it can apply out of the box to skip virtual-dom rendering entirely. If you pass a signal directly into JSX, it will bind directly to the DOM `Text` node that is created and update that whenever the signal changes. ```js import { signal } from "@preact/signals-react"; const count = signal(0); // Unoptimized: Will trigger the surrounding // component to re-render function Counter() { returnValue: {count.value}
; } // Optimized: Will update the text node directly function Counter() { return (<>Value: {count}>
); } ``` To opt into this optimization, simply pass the signal directly instead of accessing the `.value` property. > **Note** > The content is wrapped in a React Fragment due to React 18's newer, more strict children types. ### Utility Components and Hooks The `@preact/signals-react/utils` package provides additional utility components and hooks to make working with signals even easier. #### Show Component The `Show` component provides a declarative way to conditionally render content based on a signal's value. ```js import { Show } from "@preact/signals-react/utils"; import { signal } from "@preact/signals-react"; const isVisible = signal(false); function App() { return (Now you see me!
The value is {value}
}