useState vs useRef- The Ultimate dilemma.

React hooks are widely used throughout the globe. Despite the ease and comfort that they offer, hooks are often mis/partially understood and implemented poorly, resulting in poor quality code. useState and useRef are two of the most used/confused hooks. Let’s declutter them.

useState hook enables us to create a state variable of a component. This state variable holds dynamic data which has the ability to change as per user’s interaction. It takes a parameter (the initial value) and returns an array consisting of a variable (state) and a function to mutate that variable (stateUpdater).

Following is an example of it’s initialization:

[React] const [count, setCount]=useState(0); //count is the name of the variable. //setCount is the name of the function to modify value of count. //Any name could be used for the variable and the function modifying it. //0 is the value count is initialized with. [React]

useRef hook empowers us with a way to manipulate DOM elements directly. It takes a parameter (the initial value ) and returns a mutable ref object whose .current property is initialized to the initial value.

Following is an example of it’s initialization:

[React] const elementRef=useRef(0); //elementRef is the name of the ref to be created. //Any name could be used for the variable and the function modifying it. //0 is the value count is initialized with. [React]

Too much theory, let’s see some example and grasp it better.

https://vectoscalar.com/media/6b900d1b5baa38680f77966fc65bd27d
https://vectoscalar.com/media/adbea99cbb0e25cfb04c14fc2be488d9

In the above examples we have created an input field, and a submit button to hold the response filled. As is evident from the above examples, useState causes the component to re render each time the input is modified, but useRef doesn’t. In the examples employed, we have taken a single component with just handful elements which would not make a noticeable difference. However, in real world scenarios, the app may consists of numerous children, which further have children. In such cases, re rendering a parent component is a very expensive DOM manipulation. Hence, further denting the performance of the application.

Latest

SENTRY integration in your React Native App for Error/Crash tracking

Sentry captures data by using an SDK within your...

Recall the concepts of useCallback.

useCallback hook is one of the best hooks offered...

Value of Comments in the code!!

During my journey of Software Development, I am always...

YOLO:Bullet Paced Algorithm

http://sh017.hostgator.tempwebhost.net/media/33949d0e61af4b50f374c534713f56b3 According to world health organization, more than 1.35 million...

Featured

Developing Enterprise Application in Node.js – CJS Vs ESM

Node.js is a popular runtime environment for building server-side...

Integrating your web react applications with their React Native(android and IOS) apps using QR code

Integrating a web application with Android and iOS apps...

YOLO: Bullet Paced Algorithm – popular choice for object detection in autonomous vehicles 

According to world health organization, more than 1.35 million...

How to Determine Whether a Component Is Present in ViewPort

Hi guys, in this article we will learn about how to check the position of a component in the viewport, i.e., the viewable area...

Best Practices for React State Management

React state management is a crucial aspect of building scalable and maintainable applications with React. State management helps in keeping track of data that...

Error Handling in React Js

Errors are bound to happen in our applications, whether they’re server-related errors, edge cases, api calling issues or others. As such, many methods have...

LEAVE A REPLY

Please enter your comment!
Please enter your name here