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
87.7
Supply Chain
97.6
Quality
90.2
Maintenance
100
Vulnerability
100
License
@preact/signals-react@3.0.1
Published on 13 Jan 2025
@preact/signals@2.0.1
Published on 13 Jan 2025
@preact/signals@1.3.2
Published on 13 Jan 2025
@preact/signals-react-transform@0.5.1
Published on 02 Jan 2025
@preact/signals-react@3.0.0
Published on 02 Jan 2025
@preact/signals-react-transform@0.5.0
Published on 17 Dec 2024
TypeScript (94.54%)
JavaScript (5.38%)
Dockerfile (0.06%)
Shell (0.03%)
Total Downloads
522,595
Last Day
4,745
Last Week
20,573
Last Month
89,487
Last Year
518,566
3,901 Stars
806 Commits
97 Forks
26 Watching
32 Branches
55 Contributors
Minified
Minified + Gzipped
Latest Version
0.5.1
Package Id
@preact/signals-react-transform@0.5.1
Unpacked Size
243.62 kB
Size
52.43 kB
File Count
14
NPM Version
10.8.2
Node Version
18.20.5
Publised On
02 Jan 2025
Cumulative downloads
Total Downloads
Last day
16.8%
4,745
Compared to previous day
Last week
-12.7%
20,573
Compared to previous week
Last month
12.4%
89,487
Compared to previous month
Last year
12,770.8%
518,566
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};
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.