Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@preact/signals-react
Advanced tools
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
The React integration can be installed via:
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.
Install the Babel transform package (npm i --save-dev @preact/signals-react-transform
) and add the following to your Babel config:
{
"plugins": [["module:@preact/signals-react-transform"]]
}
This will automatically transform your components to be reactive. You can then use signals directly inside your components.
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>;
}
See the Readme for the Babel plugin for more details about how the transform works and configuring it.
useSignals
hookIf you can't use the Babel transform, you can directly call the useSignals
hook to make your components reactive.
import { useSignals } from "@preact/signals-react/runtime";
const count = signal(0);
function CounterValue() {
useSignals();
return <p>Value: {count.value}</p>;
}
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.
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 (
<button onClick={() => count.value++}>
Value: {count.value}, value x 2 = {double.value}
</button>
);
}
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.
import { signal } from "@preact/signals-react";
const count = signal(0);
// Unoptimized: Will trigger the surrounding
// component to re-render
function Counter() {
return <p>Value: {count.value}</p>;
}
// Optimized: Will update the text node directly
function Counter() {
return (
<p>
<>Value: {count}</>
</p>
);
}
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.
This version of React integration does not support passing signals as DOM attributes. Support for this may be added at a later date.
Using signals into render props is not recommended. In this situation, the component that reads the signal is the component that calls the render prop, which may or may not be hooked up to track signals. For example:
const count = signal(0);
function ShowCount({ getCount }) {
return <div>{getCount()}</div>;
}
function App() {
return <ShowCount getCount={() => count.value} />;
}
Here, the ShowCount
component is the one that accesses count.value
at runtime since it invokes getCount
, so it needs to be hooked up to track signals. However, since it doesn't statically access the signal, the Babel transform won't transform it by default. One fix is to set mode: all
in the Babel plugin's config, which will transform all components. Another workaround is put the return of the render prop into it's own component and then return that from your render prop. In the following example, the Count
component statically accesses the signal, so it will be transformed by default.
const count = signal(0);
function ShowCount({ getCount }) {
return <div>{getCount()}</div>;
}
const Count = () => <>{count.value}</>;
function App() {
return <ShowCount getCount={() => <Count />} />;
}
Similar issues exist with using object getters & setters. Since the it isn't easily statically analyzable that a getter or setter is backed by a signal, the Babel plugin may miss some components that use signals in this way. Similarly, setting Babel's plugin to mode: all
will fix this issue.
MIT
, see the LICENSE file.
FAQs
Manage state with style in React
The npm package @preact/signals-react receives a total of 22,661 weekly downloads. As such, @preact/signals-react popularity was classified as popular.
We found that @preact/signals-react 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.