Introduction to Signals In JS: An Overview and Advantages

Search for a command to run...

Nice article. One question though - Couldn't we achieve the same results with useRefs in React?
How are signal different? or other advantage can signal provide?
Hey great article, but as per Dan Abramov React isn't really interested in this signals approach but sounds like they might try to do something similar.
https://twitter.com/_rschristian/status/1567888708577132546
AWS Simple Storage Service(S3) is a highly secure and infinitely scalable cloud-based storage solution. It operates on an object-based storage model, allowing users to store diverse file types such as images, videos, and documents as individual objec...

Authentication and authorization are crucial aspects of modern web applications. Authentication ensures that the user is valid and verifies the user's identity while authorization determines user permission and access level. Authorisation is performe...

Six years ago when I started as a developer, I used shared hosting with cPanel to deploy my client's websites. Although it was an average experience, it was very cost-effective. Some of my old sites are still hosted in shared hosting services. Those ...

Disclaimer: Well, as things stand now, I just want to let you know that this blog post was not generated by ChatGPT. ChatGPT was used to correct the grammar and replace some of the words to make it more readable, but the ideas and opinions expressed ...

I recently noticed a surge in traffic to my old article How to deploy your full-stack application for free? which led me to believe that it has helped many people who are searching for free tools to host their applications. It's always great to see t...

In the world of JavaScript-based frameworks and libraries, Signals are a relatively new addition. It was first introduced in frameworks like Preact, React, Qwik, and SolidJS some time ago, however, Angular's recent implementation of Signals in its latest version has caused a significant buzz and increased its popularity. This blog post will explore what Signals are, how they make state management more convenient, and why they matter in modern web development.
In this blog, we will be using the React examples to show the use of the Signals. We will discuss how React rendering works without Signals, and how incorporating Signals can significantly enhance your application's performance.
Before understanding signals, it's important to first understand how React handles state changes. For those unfamiliar with it, here's a brief overview: a general working process in React is that, if you have a component and it has a state, then whenever any state changes in your component then that component and its all child component will re-render. This process occurs in a Virtual DOM and React at its core will check for the UI and its data difference between the Virtual DOM and the main DOM. Any differences among them are then updated in the main DOM. This is how React ensures that users see the most up-to-date UI.
Let's take a look at an example in this Code Sandbox: we have a simple application that increases the count value when the "Increase Count" button is clicked, and resets the counter when "Reset Count" is clicked. Additionally, we're keeping track of the re-render counter, which increases every time the component re-renders. You'll notice that every time the "Increase Count" or "Reset Count" buttons are clicked, the component re-render count also increases. This means that the entire component is being re-rendered with each state change. While this is just a small UI component, imagine the same scenario with larger UI components that undergo numerous state changes. Yes, that's right, this can lead to poor application performance.
This is where Signals offer a solution to the issue. Similar to state, Signals allow you to store a value for your UI, but the key difference is that Signals can be updated without triggering a re-render of the entire component. Instead, components access the Signal directly, without rendering. This allows us to avoid costly rendering work and jump straight to the components in the tree that actually access the Signal's .value property.
Let's explore an example Code Sandbox below. It contains the same code as the previous example but instead of the state, it uses Signals. Try clicking the "Increase Count" and "Reset Count" buttons and watch the results. Notice anything different? That's right, the Component Render Count doesn't increase. This is because the component isn't re-rendered. Instead, when the count changes, the Signals inject the updated value directly where it's used in the component without triggering a re-render.
You can also pass Signals to your child component from the parent. This allows for a much more efficient application, especially when dealing with large UI components with many state changes.
React state management could be challenging. React renders your components whenever a state changes. This can lead to expensive rendering work and harm application performance, especially with large projects.
Signals in React (also available in other JS frameworks) offer a solution to this issue by allowing you to store a value for your UI and update it without triggering a component re-render. Components access the Signal directly, without rendering, which can significantly improve application performance.
Implementing Signals in your UI application can help improve its overall efficiency, speed, and performance, which could make your application more appealing to the user.
In this article, we discussed, how React component works, what triggers re-rendering in React, and how to prevent unnecessary re-rendering using Signals to boost the application efficiency.
Whether you are a beginner or you are a professional, I am sure this article has helped you understand how React signals could help boost your UI performance. If you found this information useful, please consider sharing it with friends or colleagues who may also benefit from it. If you have any questions or would like to discuss the topic further, you can reach out to me on Twitter at https://twitter.com/aabiscodes or LinkedIn at https://www.linkedin.com/in/aabis7/.