A reader of my blog reached me on Facebook with an interesting question. He said his teammates, no matter the situation, were wrapping every callback function inside Show
"Every callback function should be memoized to prevent useless re-rendering of child components that use the callback function" is the reasoning of his teammates. This reasoning is far from the truth. Such usage of In this post, I'm going to explain how to use correctly 1. Understanding functions equality checkBefore diving into Functions in JavaScript are first-class citizens, meaning that a function is a regular object. The function object can be returned by other functions, be compared, etc.: anything you can do with an object. Let's write a function 3 that returns functions that sum numbers:
Try the demo. 4 and 5 are functions that sum two numbers. They've been created by the 3 function. The functions 4 and 5 share the same code source, but they are different function objects. Comparing them 9 evaluates to 0. That's just how JavaScript objects work. An object (including a function object) only to itself. 2. The purpose of useCallback()Different function objects sharing the same code are often created inside React components:
1 is a different function object on every rendering of 2. Because inline functions are cheap, the re-creation of functions on each rendering is not a problem. A few inline functions per component are acceptable. But in some cases you need to maintain a single function instance between renderings:
That's when 4 is helpful: given the same dependency values 5, the hook returns the same function instance between renderings (aka memoization):
1 variable has always the same callback function object between renderings of 2. 3. A good use caseYou have a component 8 that renders a big list of items:
The list could be big, maybe hundreds of items. To prevent useless list re-renderings, you wrap it into 9. The parent component of 0 provides a handler function to know when an item is clicked:
1 callback is memoized by useCallback() . As long as 3 is the same, useCallback() returns the same function object.When 5 component re-renders, 1 function object remains the same and doesn't break the memoization of 0. That was a good use case of 4. A bad use caseLet's look at another example:
The first problem is that 2 renders. This already reduces the render performance. The second problem is that using 5 of 3 in sync with what you're using inside the memoized callback. Does it worth using 5 component is light, and its re-rendering doesn't create performance issues. The optimization costs more than not having the optimization. Simply accept that rendering creates new function objects:
5. SummaryWhen thinking about performance tweaks, recall the statement:
When deciding to use an optimization technique, such as memoization with
To enable the memoization of the entire component output I recommend checking my post Use React.memo() wisely. Do you know use cases that are worth using Like the post? Please share!Suggest Improvement Quality posts into your inboxI regularly publish posts containing:
Subscribe to my newsletter to get them right into your inbox. SubscribeJoin 6946 other subscribers. About Dmitri PavlutinTech writer and coach. My daily routine consists of (but not limited to) drinking coffee, coding, writing, coaching, overcoming boredom 😉. How do you call a callback function in TypeScript?Callback in Angular2/TypeScript
In service class, you can create method which can accept parameter of function type. After method is done processing the request, it will execute the callback function with data that needs to be passed.
How do you pass a callback function in React hook?The useCallback hook is used when you have a component in which the child is rerendering again and again without need. Pass an inline callback and an array of dependencies. useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed.
How do you pass a function as props in React TypeScript?Create a function in the parent component that accepts a callback as an argument. Pass the function to the child component as a prop. In the child component, create the function you want to pass UP to the parent. Call the parent function prop in the child component with the child function as the argument.
How do you pass a function to another function in TypeScript?Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.
|