最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I wrap all my components with React.memo() if it is not expecting any props? - Stack Overflow

programmeradmin1浏览0评论

While it is obvious why React does not perform React.memo to all its functional ponent by default, and we should ourself wrap our functional ponents as and when needed.

In my case, I have a big react project with a structure like this:

const MyBigApp = () => {
  const shouldShowX = useSelector(getShouldShowX);
  const shouldShowY = useSelector(getShouldShowY);
  // Many more selectors

  return (
    <>
      {shouldShowX && <ComplexComponentX />}
      {shouldShowY && <ComplexComponentY />}
      {/* many more ponents based on different selectors like above */}
   </>
  );
};

All my business logic is inside redux and the ponents use useSelector internally to get data from the store.

Does it make sense to wrap all my child ponents with React.memo, as a change in any selector at root level causes my entire app to re-render?

Earlier with connect we were automatically getting a memoized ponent which was paring custom props and store value passed as props to the ponent, so should we now manually always do React.memo while using useSelector in a ponent not receiving any props?

While it is obvious why React does not perform React.memo to all its functional ponent by default, and we should ourself wrap our functional ponents as and when needed.

In my case, I have a big react project with a structure like this:

const MyBigApp = () => {
  const shouldShowX = useSelector(getShouldShowX);
  const shouldShowY = useSelector(getShouldShowY);
  // Many more selectors

  return (
    <>
      {shouldShowX && <ComplexComponentX />}
      {shouldShowY && <ComplexComponentY />}
      {/* many more ponents based on different selectors like above */}
   </>
  );
};

All my business logic is inside redux and the ponents use useSelector internally to get data from the store.

Does it make sense to wrap all my child ponents with React.memo, as a change in any selector at root level causes my entire app to re-render?

Earlier with connect we were automatically getting a memoized ponent which was paring custom props and store value passed as props to the ponent, so should we now manually always do React.memo while using useSelector in a ponent not receiving any props?

Share Improve this question edited Aug 18, 2020 at 15:04 Ashish asked Aug 17, 2020 at 20:04 AshishAshish 4,33019 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I would argue that you should consider using React.memo for those child ponents that don't accept any props. I highly remend this article: https://dmitripavlutin./use-react-memo-wisely/

2.1 Component renders often with the same props
The best case of wrapping a ponent in React.memo() is when you expect the functional ponent to render often and usually with the same props.

Since the ponents you're asking about don't accept any props, that means the props will never change. Therefore, it stands to reason that using React.memo could avoid render cycles. Note this important caveat from the same article:

5. React.memo() is a performance hint
Strictly, React uses memoization as a performance hint.

While in most situations React avoids rendering a memoized ponent, you shouldn’t count on that to prevent rendering.

发布评论

评论列表(0)

  1. 暂无评论