An Interest In:
Web News this Week
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
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,
- Signals do not trigger re-renders of whole component, instead they just update the value in place.
- 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> </>)}
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
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To