Menu

Post image 1
Post image 2
1 / 2
0

useMemo in React — Why It Exists and How to Use It Simply

DEV Community·DHANRAJ S·18 days ago
#aDhk7ZGn
Reading 0:00
15s threshold

Hey! Let me ask you something first. Every time state changes in React — what happens? The component re-renders. Every line inside it runs again from top to bottom. Most of the time — that is fine. But what if one of those lines is doing a heavy calculation? Something that takes real time to compute? That calculation runs again. Every single render. Even when nothing related to it changed. That is the problem useMemo fixes. 1. What Is the Problem? Let us see it in code. import { useState } from " react " ; function App () { const [ count , setCount ] = useState ( 0 ); const [ input , setInput ] = useState ( "" ); function slowCalculation ( num ) { console . log ( " Calculating...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More