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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- {name: 'config', type: 'basic'} -->

### 要回答的问题

<!--
说明:
描述要回答的问题
比如:
实现水平垂直居中布局
-->

简述 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


### 最佳答案评选标准

<!--
说明:
指明回答问题的方向、顺序、步骤,引导答题者规范答题
比如:
1. 请从以下角度回答该问题:
- 元素定宽高
- 元素不定宽高

2. 多种实现方式是加分项
3. 对详细的描述、代码示例是加分项
-->