1

React gives a way to memoize the components using React.memo which actually stops the component re-render if there is no change in props.

import React, { memo } from 'react';

interface ComponentNameProps {

}
const ComponentName:React.FC<ComponentNameProps> = props => {
    const {} = props;
    return (
        <div>
        </div>
    )
}
export default memo(ComponentName)

Do we put all our component into memo, will it be a good practise?

Varun Sukheja
  • 6,170
  • 5
  • 51
  • 93
  • `React.memo` is used to memoize heavy calculations. I don't think what you are trying to do is **good practice**. – Ayush Gupta Aug 20 '21 at 07:18
  • This might be useful, checkout this answer https://stackoverflow.com/questions/53074551/when-should-you-not-use-react-memo – Awais Aug 20 '21 at 07:21

2 Answers2

0

Well, if you use React.memo, everytime you use the component react is gonna run a compare function to see if the props have changed; so if redeclaring your function is less expensive, then you are hurting the performance, rather than improving it. so it's a tradeoff that sometimes is worth making, but not for every component.

e.a.
  • 919
  • 4
  • 9
-2

No, it is not a good practice. In fact, overusing useMemo can hurt your application more (performance-wise) than the re-renders.

Please use wisely and with caution.

Read more about it here.

Nitsan Cohen
  • 619
  • 3
  • 6
  • 2
    `useMemo` is not the same thing as `React.memo`. Also the performance considerations are [quite different](https://stackoverflow.com/a/63405621/4961158). – inwerpsel Jul 24 '22 at 19:58