Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 14, 2022 05:09 am GMT

Introducing Signals in React

We have all heard/used states in React. It is a way to observe a value and its change to update the UI accordingly without triggering full page refresh. It is very useful but it comes at a cost, State change triggers re-rendering of the component, Which can hit the performance.

Preact

Preact is a 3kb alternative library which uses same API as React. But since its smaller, it ships less code to the browser.
Recently, They have announced a new package which is available to install react as well called Signals.
Docs

According to Docs:

What makes Signals unique is that state changes automatically update components and UI in the most efficient way possible. Automatic state binding and dependency tracking allows Signals to provide excellent ergonomics and productivity while eliminating the most common state management footguns.

What signal does is, It is a object containing a property .value, Once defined that signal/object remains the same but its value can change and when it does it notifies this change so that reference to that value can be updated.

Couple of things that make this interesting are,

  1. Signals do not trigger re-renders of whole component, instead they just update the value in place.
  2. They can be defined globally and imported in other components, So whenever value for specific signal changes, It can update the UI with the value throughout the application.

To Install Signal in React

npm install @preact/signals-react

Simple Use case

import {signal} from '@preact/signals-react'//create a signalconst count = signal(0)const App = () =>{ return(   <>     <p>{count.value}</p>   </>)}

Preact signal Github

So try it and let me know what you guys think.

Cheers and Happy Coding


Original Link: https://dev.to/ishan_parlikar/introducing-signals-in-react-28k6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To