perf(useDebounceEffect & useThrottleEffect): effectCallback called that do not rely on rerender #2399
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.
🤔 这个变动的性质是?
🔗 相关 Issue
#2398
💡 需求背景和解决方案
问题
这可能不是一个 bug,是一个性能问题。使用useDebounceEffect & useThrottleEffect ,当 callback 被执行前,多了一次额外的 rerender,即下图红色圈起来的部分。demo请看这里
实现
上面的问题是由于执行 effectCallback 要依赖 state 的更新(对应源码
useUpdateEffect(effect, [flag])
),这个机制引起的。这样做的缺点是想要执行 effectCallback,需要先 setState(xx) ,则会引起多一次额外的 rerender。我认为可以通过一些方式解决此问题,但是需要注意不能丢失了 cleanup 功能(参见文档)。
📝 更新日志
☑️ 请求合并前的自查清单