
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@preact/signals-react-transform
Advanced tools
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.
npm install @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. This babel transform is one of a couple different ways to use signals in React. To see other ways, including integrations that don't require a build step, see the Signals React documentation.
Then, setup the transform plugin in your Babel config:
// babel.config.js
module.exports = {
plugins: [["@preact/signals-react-transform"]],
};
Here is an example of a component using signals:
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 <p>Value: {count.value}</p>;
}
After the babel transform runs, it'll look something like:
import { signal, useSignals } from "@preact/signals-react";
const count = signal(0);
function CounterValue() {
const effect = useSignals();
try {
// Whenever the `count` signal is updated, we'll
// re-render this component automatically for you
return <p>Value: {count.value}</p>;
} finally {
effect.endTracking();
}
}
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 endTracking
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() {}
), contains JSX, and is declared at module scope..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 @trackSignals
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 @noTrackSignals
.
// This function will be transformed
/** @trackSignals */
function MyComponent() {
return createElement("h1", null, signal.value);
}
// This function will not be transformed
/** @noTrackSignals */
function MyComponent() {
return <p>{signal.value}</p>;
}
Note, this plugin will not transform higher-order components (HOCs) that wrap other components. If you have an HOC that uses signals, you can use the @trackSignals
comment to transform the body of the higher-order component.
mode
The mode
option enables you to control how the plugin transforms your code. There are two 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 @trackSignals
. This is useful if you want to manually control which functions are transformed.// babel.config.js
module.exports = {
plugins: [
[
"@preact/signals-react-transform",
{
mode: "manual",
},
],
],
};
MIT
, see the LICENSE file.
FAQs
Manage state with style in React
The npm package @preact/signals-react-transform receives a total of 18,041 weekly downloads. As such, @preact/signals-react-transform popularity was classified as popular.
We found that @preact/signals-react-transform demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.