Languages
[Edit]
EN

React - memo on event props (on event functions)

9 points
Created by:
Frida-Timms
667

In this article, we would like to show how to use memo on event props (on event functions) in React.

Motivation

Edit

When functions references change in component props the component gets re-rendering. It happens also when memo() is used, even the method body doesn't change, because of new function creation. We can solve the problem by using useCallback() or useMemo() on that functions but it complicates source code.

React doesn't provide memo hook that prevents against re-rendering only on selected event props. It is easy to create own one hook, what was shown in this article.

Solution

Edit

In this article we propose simple way how to create memo on selected props that have functions as values by adding $ prefix to props names.

Practical example

Edit

App.jsx file:

 

pipe.jsx file:

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join