-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please add more detail on useMemo hooks? #178
Comments
useMemo is a React hook that is used to optimize the performance of a component by memoizing (caching) the values of expensive function calls. It takes in two arguments: a function and an array of dependencies. The function will only be re-evaluated if one of the dependencies has changed. For example, let's say you have a component that renders a list of items from an array. You want to add some additional logic to each item in the list, but this logic is expensive and you don't want it to run every time the component renders. You can use useMemo to memoize this logic so that it only runs when one of the dependencies changes: |
useMemo hook definiton:useMemo is a React hook that allows you to memoize (or cache) the result of a function so that it only gets recomputed when the inputs to that function change, useMemo basically optimizes the performance of a component. Understanding useMemo with an easy example Hope this helps |
Performance Hooks: A common way to optimize re-rendering performance is to skip unnecessary work. For example, you can tell React to reuse a cached calculation or to skip a re-render if the data has not changed since the previous render. To skip calculations and unnecessary re-rendering, use one of these Hooks:
Both hooks are used to increase the performance of our application. Let's take an example:
Here expensiveCalculation function will be called on every render even it does not has any direct relation to todos, it will cause performance issue. Solution: useMemo hook useMemo hook:
Now expensiveCalculation function will only be called at first render and on every count change. |
Hi Sir,
Can you add a new point on how and why useMemo() is used in ReactJS with example. This is an important concept and without this, you can't undestand pure components.
The text was updated successfully, but these errors were encountered: