Gathering detailed insights and metrics for @preact/signals-react-transform
Gathering detailed insights and metrics for @preact/signals-react-transform
Gathering detailed insights and metrics for @preact/signals-react-transform
Gathering detailed insights and metrics for @preact/signals-react-transform
Manage state with style in every framework
npm install @preact/signals-react-transform
Typescript
Module System
Node Version
NPM Version
@preact/signals@2.2.1
Updated on Jun 29, 2025
@preact/signals-react@3.2.1
Updated on Jun 29, 2025
@preact/signals-core@1.11.0
Updated on Jun 29, 2025
@preact/signals-core@1.10.0
Updated on Jun 11, 2025
@preact/signals-react-transform@0.5.2
Updated on Jun 04, 2025
@preact/signals-core@1.9.0
Updated on May 29, 2025
TypeScript (95.26%)
JavaScript (4.66%)
Dockerfile (0.05%)
Shell (0.02%)
Total Downloads
1,000,318
Last Day
969
Last Week
29,096
Last Month
125,041
Last Year
942,583
MIT License
4,118 Stars
861 Commits
111 Forks
25 Watchers
35 Branches
59 Contributors
Updated on Jul 03, 2025
Minified
Minified + Gzipped
Latest Version
0.5.2
Package Id
@preact/signals-react-transform@0.5.2
Unpacked Size
308.96 kB
Size
65.57 kB
File Count
14
NPM Version
10.8.2
Node Version
18.20.8
Published on
Jun 04, 2025
Cumulative downloads
Total Downloads
Last Day
-9.9%
969
Compared to previous day
Last Week
-6.5%
29,096
Compared to previous week
Last Month
7.5%
125,041
Compared to previous month
Last Year
1,532.6%
942,583
Compared to previous year
5
2
A Babel plugin to transform React components to automatically subscribe to Preact Signals.
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 i --save-dev @preact/signals-react-transform
This package works with the @preact/signals-react
package to integrate signals into React. You use the @preact/signals-react
package to setup and access signals inside your components and this package is one way to automatically subscribe your components to rerender when the signals you use change. To understand how to use signals in your components, check out the Signals React documentation.
To setup the transform plugin, add the following to your Babel config:
1// babel.config.js 2module.exports = { 3 plugins: [["module:@preact/signals-react-transform"]], 4};
Here is an example of a component using signals:
1import { signal } from "@preact/signals-react"; 2 3const count = signal(0); 4 5function CounterValue() { 6 // Whenever the `count` signal is updated, we'll 7 // re-render this component automatically for you 8 return <p>Value: {count.value}</p>; 9}
After the babel transform runs, it'll look something like:
1import { signal, useSignals } from "@preact/signals-react"; 2 3const count = signal(0); 4 5function CounterValue() { 6 const store = useSignals(1); 7 try { 8 // Whenever the `count` signal is updated, we'll 9 // re-render this component automatically for you 10 return <p>Value: {count.value}</p>; 11 } finally { 12 store.f(); 13 } 14}
The useSignals
hook setups the machinery to observe what signals are used inside the component and then automatically re-render the component when those signals change. The f()
function notifies the tracking mechanism that this component has finished rendering. When your component unmounts, it also unsubscribes from all signals it was using.
Fundamentally, this Babel transform needs to answer two questions in order to know whether to transform a function:
Currently we use the following heuristics to answer these questions:
function MyComponent() {}
) and contains JSX..value
(i.e. something.value
), we assume it's a signal.If your function/component meets these criteria, this plugin will transform it. If not, it will be left alone. If you have a function that uses signals but does not meet these criteria (e.g. a function that manually calls createElement
instead of using JSX), you can add a comment with the string @useSignals
to instruct this plugin to transform this function. You can also manually opt-out of transforming a function by adding a comment with the string @noUseSignals
.
1// This function will be transformed 2/** @useSignals */ 3function MyComponent() { 4 return createElement("h1", null, signal.value); 5} 6 7// This function will not be transformed 8/** @noUseSignals */ 9function MyComponent() { 10 return <p>{signal.value}</p>; 11}
mode
The mode
option enables you to control how the plugin transforms your code. There are three modes:
mode: "auto"
(default): This mode will automatically transform any function that meets the criteria described above. This is the easiest way to get started with signals.mode: "manual"
: This mode will only transform functions that have a comment with the string @useSignals
. This is useful if you want to manually control which functions are transformed.mode: "all"
: This mode will transform all functions that appear to be Components, regardless of whether or not they use signals. This is useful if you are starting a new project and want to use signals everywhere.1// babel.config.js 2module.exports = { 3 plugins: [ 4 [ 5 "@preact/signals-react-transform", 6 { 7 mode: "manual", 8 }, 9 ], 10 ], 11};
importSource
The importSource
option enables you to control where the useSignals
hook is imported from. By default, it will import from @preact/signals-react
. This is useful if you want to wrap the exports of the @preact/signals-react
package to provide customized behavior or if you want to use a different package entirely. Note: if you use a different package, you'll need to make sure that it exports a useSignals
hook with the same API & behavior as the one in @preact/signals-react
.
1// babel.config.js 2module.exports = { 3 plugins: [ 4 [ 5 "@preact/signals-react-transform", 6 { 7 importSource: "my-signals-package", 8 }, 9 ], 10 ], 11};
detectTransformedJSX
When enabled, alternative methods like React.createElement
and jsx-runtime
will also trigger transformation.
This can be especially useful when npm depencendies are being transpiled as those generally don't contain JSX since they have alredy been transpiled to JavaScript.
1// babel.config.js 2module.exports = { 3 plugins: [ 4 [ 5 "@preact/signals-react-transform", 6 { 7 detectTransformedJSX: tue, 8 }, 9 ], 10 ], 11};
This plugin uses the debug
package to log information about what it's doing. To enable logging, set the DEBUG
environment variable to signals:react-transform:*
.
MIT
, see the LICENSE file.
No vulnerabilities found.
No security vulnerabilities found.