Logo

dev-resources.site

for different kinds of informations.

Stop using useState for everything

Published at
5/8/2023
Categories
react
useref
usestate
webdev
Author
dougyoshii
Categories
4 categories in total
react
open
useref
open
usestate
open
webdev
open
Author
10 person written this
dougyoshii
open
Stop using useState for everything

The useState hook was created to help us to store data and update the UI all the times that the data changes. But, every time the state changes, it causes a re-render on your app and this could be a problem.

Let's take a look on the code below

Image showing a react code with useState

You might be thinking: “It works, there's nothing wrong, right?” - The answer is: no.

The onChange function will set a state every time the user presses a key on keyboard. It means the app will re-render every time the user types a letter.

And as we can see, our app is re-rendering unnecessary. 👇🏽

Image showing an app re-rendering many times

*So, we gonna fix that using the useRef hook.

Why?

First: the useRef allow us to get the input value without setting a state every time the user types a letter.

Second: We gonna use the useRef because we don't need to update the UI. If wee need to update UI, we must be using useState.

Image showing how to use useRef

As you can see, we don't need to set a new state on the onChange event and our application just render one time, like the picture below:

Image showing the result of using useRef

“But Douglas, the app is rendering twice 😭*”* - Yes, the React Strict mode is enabled and it renders twice just to detect any problem in the app. It only runs on debug mode, so in production will gonna be safety 🙂

So, it works! Your app is happy. Now we have the entire input value without rendering so many times.

Bye 🙂

usestate Article's
30 articles in total
Favicon
useState e useEffect
Favicon
Mastering React's useState Hook: The Basics and Advanced Use Cases
Favicon
Understanding useState in TypeScript React
Favicon
A Beginner's Guide to useState in React
Favicon
Mastering React Hooks: useState and useEffect
Favicon
Managing State in react using different method, & understand batching
Favicon
Hooks Behind The Scenes 2, useState!!
Favicon
useState Hook Explained
Favicon
Exploring State Management in React: One-Way Data Binding, State Lift-Up, Prop Drilling, and Handling Complex States
Favicon
Diving into React.js
Favicon
What is useState?
Favicon
Avoiding Common useState() Mistakes in React
Favicon
Simplifying State Management in React with Zustand
Favicon
Having trouble with react component hooks...
Favicon
useState( )
Favicon
Create a modified useState hook to get previous value
Favicon
UseState why?
Favicon
Part 2 - Mastering the useState Hook
Favicon
Part 1 - Getting Started with React Hooks
Favicon
ugly useState
Favicon
Unlocking the Power of React Hooks
Favicon
Mastering 'useState' in React with TypeScript: 5 Different Use-Cases for 'useState'
Favicon
React.useRefの理解を含めるエクササイズ
Favicon
Toggle Feature for a Drop-down list on React using useState();
Favicon
useContext
Favicon
A Beginner's Guide to Using Fetch in React: Making Data Requests Made Easy
Favicon
Alterações de páginas de forma dinâmica com o UseState
Favicon
Stop using useState for everything
Favicon
useState: la caja de juguetes
Favicon
Detaylı React Hooks Kullanımı: useState

Featured ones: