• What we solve ›
  • Focus areas ›
  • Services
  • Work
  • About
  • Contact
Home Blog Simplify your React application with Signals
Simplify your React application with Signals

Introduction

While hooks are pretty cool, they can be quite difficult to master. There are a lot of rules and special techniques required to use them correctly (can only be used inside components, cannot be conditionally called, some hooks are sometimes run twice etc). Using them incorrectly can lead to bugs and degraded performance.

Signals

Signals offer a pragmatic alternative to state handling and events. They're performant out of the box, very lightweight, and are easy to use and to reason about.

Let's try a quick example!

First install the React Signals package from Preact:

npm install @preact/signals-react

To show how Signals work in an example, let's create a simple component that renders a button with a small counter in it. In order to showcase some of the functionality of Signals we'll make it a little spicy!

import { effect, signal } from '@preact/signals-react';

// Create our signal and set the original value to `0`.
const count = signal(0);

// Every time the value of the signal changes, log to console. N.B. that we
// didn't specify any dependencies! Signals has all the information it needs.
effect(() => console.log(count.value));

// Every 5 seconds, change the count to a random number -- from outside of the
// component no less!
setInterval(() => {
  count.value = Math.random();
}, 5000);

const Counter = () => (
	<button onClick={() => count.value++}>{count}</button>
);

export default Counter;
export { count };

If we want to access the signal from another component we can simply import it!

import { count } from "./Counter";

function DisplayCount() {
  return (
    <div>Count: {count}</div>
  );
}

export default DisplayCount;

Super-simple, yet very powerful! You can read more about Signals in this awesome article by Preact.

Author
Gustaf Sjöberg
Published at
January 11, 2024
Category
React
Share
FB , LinkedIn , X
A mountain range
Find your way
Tell us where you want to go, and we'll explore the best ways to get you there.
Get in touch
Simplify complex
energy operations
General
  • Home
  • Services
  • Work
  • About
  • Contact
  • Blog
What we solve
  • Data collection
  • System integration
  • Energy data
  • Control & operations
  • Forecasting & decision support
  • Applications & visualization
Focus areas
  • District heating & cooling
  • Batteries & flexibility
  • Electricity retail & energy services
  • Water utilities
  • Industry & property
  • Electricity grids
Elsewhere
  • Linkedin
  • Instagram
  • Teamtailor
  • GitHub
  • Codeberg
© Helicon Technologies AB
Privacy Terms of use
  • Home
  • What we solve
    • Data collection
    • System integration
    • Energy data
    • Control & operations
    • Forecasting & decision support
    • Applications & visualization
  • Focus areas
    • District heating & cooling
    • Batteries & flexibility
    • Electricity retail & energy services
    • Water utilities
    • Industry & property
    • Electricity grids
  • Services
  • Work
  • About
  • Blog
Simplify complex
energy operations
Contact us
Privacy Terms of use
Data collection
System integration
Energy data
Control & operations
Forecasting & decision support
Applications & visualization
District heating & cooling
Batteries & flexibility
Electricity retail & energy services
Water utilities
Industry & property
Electricity grids