Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add question #56

Closed
wants to merge 1 commit into from
Closed

feat: add question #56

wants to merge 1 commit into from

Conversation

a145789
Copy link

@a145789 a145789 commented Apr 12, 2022

No description provided.

实现水平垂直居中布局
-->

简述 React.memo 包裹一个组件和 useMemo 缓存一个组件有什么区别?二者是否都可以达到性能优化的效果?如果 useMemo 可以达到性能优化的效果,那么 React.memo 还有意义么?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo只能缓存一个变量,不能缓存组件

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

业务上是可以这样用的,虽然不知道这个还符不符合现在的规范
codesandbox

#15156 Option 3: One component with useMemo inside

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

奥,我理解你的意思了~~

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得这是个好问题,但是太深入React原理了。 在这里我回答下吧:
两者要达到的目的是相同的,都是为了让React走优化策略,让子组件不render
区别是:React.memo是改变优化策略的判断条件,将判断props === newProps变为浅比较props与newProps
memo不改变判断条件,只是用比较hack的方式使得props === newProps

@a145789 a145789 closed this Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants