Overall performance is an important facet in programming, particularly in significant scale application. The useMemo hook is one particular software that can be applied to enhance the performance of respond application. In this tutorial I will attempt to display how and when to use useMemo hook.
Introduction to React useMemo
useMemo is a person of the further hooks that react presents. what useMemo does is it memoize the return value of a functionality. This suggests that I executes purpose that is handed to it and remembers its return benefit.
When the software is initially renders all the functions executes and on each re-render all the features again executes. useMemo assists you to stay clear of the execution of capabilities on each individual render. useMemo only executes the function handed to it when specified problem is met.
When specified problems are not met useMemo return the earlier value of that perform and avoids executing the functionality. This can enable you enhance your respond application by avoiding expensive calculation every single time component re-renders.
useMemo accepts two parameters the very first one particular is the functionality to be executed and the second parameter is the array of dependence. Improve of any price passed in the dependency array will the useMemo hook to executed the functionality once again and recompute the memoized value. The useMemo hook will execute the functionality you passed as an argument immediately after the preliminary render by default.
// Import useMemo from React: import useMemo from 'react' export default operate Application() // useMemo syntax case in point: const memoizedValue = useMemo(() => /* perform to execute */, [/* Dependencies */]) return ( )
** If we do not pass the dependency array the purpose will execute on every render.**
const memoizedValue = useMemo(()=>/* perform to execute */)
** If we move vacant dependency array the functionality will only execute on initial render.**
const memoizedValue = useMemo(()=>/* function to execute */,)
When to use useMemo hook.
When there is an high-priced calculation likely on in the part the useMemo hook will arrive in useful. lets search into an instance
import React,useState from 'react' purpose MemoTutorial() const [count,setCount] = useState() const [name,setName] = useState("") const thousendNumber = expensiveFunction(depend) return (setCount(parseInt(worth))/> setName(value)/>) function expensiveFunction (num) console.log('Expensive Function') for (permit i=0i<1000000000i++) // Expensive function return num * 2 export default MemoTutorialthousendNumber
In the above example we have a function called expensiveFunction. Which by its name says that it is an expensive function. The expensiveFunction will take some time to return value. In the above example expensiveFunction will be executed on every render.
The only thing that is need in the expensive function is the value of count so if we follow best practices then the expensiveFunction should only run when the value of count changes. But right now the expensiveFunction will execute even if the values of name is changed.
To avoid this scenerio of running the function on every render useMemo comes into play .
import React,useState,useMemo from 'react' function MemoTutorial() const [count,setCount] = useState(0) const [name,setName] = useState("") const thousendNumber = useMemo(()=> return expensiveFunction(count) ,[count]) return (setCount(parseInt(price))/> setName(worth)/>) functionality expensiveFunction (num) console.log('Expensive Function') for (allow i=0i<1000000000i++) // Expensive function return num * 2 export default MemoTutorialthousendNumber
In the above code we have wrapped the expensiveFunction in useMemo and we have set the dependence which will cause the re computation of the expensiveFunction. In our case dependency is ** count **.
Now if you run the code you will notice that every time the value of count is changed the expensiveFunction will get executed but if we change the value name the expensiveFunction will not be executed. By restricting the needless execution of expensive functions we optimize the performance of our react app.
The React useMemo hook can be useful when you look for ways to improve performance of your React app. It can help you optimize expensive computations by memoizing output of these computations and avoiding needless executions. I hope that this tutorial helped you understand what the useMemo hook is, how it works and also how to use it.