DEV Community

Discussion on: When should you memoize in React

kurtgokhan profile image
Gökhan Kurt

I want to point out something. In your first example, if you don't memoize, the input component's addEventListener and removeEventListener will be called in every render. For a native input this may not be a big problem but if it is not an input but a custom blackboxed component, it is almost always better to memoize. Because you will not know the cost of not memoizing. Generally you should memoize things you pass as props to other components.

prateek3255 profile image
Prateek Surana Author

Yes, I agree, but it would be only helpful if the blackboxed component is a memoized component or a component that relies on the function being referentially equal on every re-render of the parent component (like it uses the function in the dependency array of a useEffect).

In any other case, it would be completely useless to memoize the function because the blackboxed component would re-render anyways if the parent component re-renders.

Also, I think the author of the blackboxed component should inform the consumer if it relies on the function being referentially equal.