Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useMemo
只能缓存一个变量,不能缓存组件There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
奥,我理解你的意思了~~
There was a problem hiding this comment.
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与newPropsmemo
不改变判断条件,只是用比较hack的方式使得props === newProps